
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Django入门与实践-第13章：表单处理 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="Authentication0.html" />
    
    
    <link rel="prev" href="AdvancedConcept2.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="GettingStarted.html">
            
                <a href="GettingStarted.html">
            
                    
                    Django入门与实践-第1章：环境搭建
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="GettingStarted-2.html">
            
                <a href="GettingStarted-2.html">
            
                    
                    Django入门与实践-第2章：创建项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="gettingstarted-3.html">
            
                <a href="gettingstarted-3.html">
            
                    
                    Django入门与实践-第3章：Hello World
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="Fundamentals-1.html">
            
                <a href="Fundamentals-1.html">
            
                    
                    Django入门与实践-第4章：系统设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="Fundamentals-2.html">
            
                <a href="Fundamentals-2.html">
            
                    
                    Django入门与实践-第5章：模型设计
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="Fundamentals-3-0.html">
            
                <a href="Fundamentals-3-0.html">
            
                    
                    Django入门与实践-第6章：第一个视图函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="Fundamentals-3-1.html">
            
                <a href="Fundamentals-3-1.html">
            
                    
                    Django入门与实践-第7章：模板引擎设置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="Fundamentals-3-1-1.html">
            
                <a href="Fundamentals-3-1-1.html">
            
                    
                    Django入门与实践-第8章：第一个单元测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="Fundamentals-3-2.html">
            
                <a href="Fundamentals-3-2.html">
            
                    
                    Django入门与实践-第9章：静态文件设置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="Fundamentals-4.html">
            
                <a href="Fundamentals-4.html">
            
                    
                    Django入门与实践-第10章：Django Admin 介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="AdvancedConcepts.html">
            
                <a href="AdvancedConcepts.html">
            
                    
                    Django入门与实践-第11章：URL 分发
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="AdvancedConcept2.html">
            
                <a href="AdvancedConcept2.html">
            
                    
                    Django入门与实践-第12章：复用模板
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.14" data-path="AdvancedConcept3.html">
            
                <a href="AdvancedConcept3.html">
            
                    
                    Django入门与实践-第13章：表单处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="Authentication0.html">
            
                <a href="Authentication0.html">
            
                    
                    Django入门与实践-第14章：用户注册
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.16" data-path="Authentication1.html">
            
                <a href="Authentication1.html">
            
                    
                    Django入门与实践-第15章：用户注销
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.17" data-path="Authentication2.html">
            
                <a href="Authentication2.html">
            
                    
                    Django入门与实践-第16章：用户登录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.18" data-path="DjangoORM0.html">
            
                <a href="DjangoORM0.html">
            
                    
                    Django入门与实践-第17章：保护视图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.19" data-path="DjangoORM1.html">
            
                <a href="DjangoORM1.html">
            
                    
                    Django入门与实践-第18章：实现主题回复列表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.20" data-path="DjangoORM2.html">
            
                <a href="DjangoORM2.html">
            
                    
                    Django入门与实践-第19章：实现主题回复功能
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.21" data-path="DjangoORM3.html">
            
                <a href="DjangoORM3.html">
            
                    
                    Django入门与实践-第20章：查询结果集
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.22" data-path="DjangoORM.html">
            
                <a href="DjangoORM.html">
            
                    
                    Django入门与实践-第21章：Django数据迁移
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.23" data-path="ClassBasedViews0.html">
            
                <a href="ClassBasedViews0.html">
            
                    
                    Django入门与实践-第22章：基于类的视图
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.24" data-path="ClassBasedViews1.html">
            
                <a href="ClassBasedViews1.html">
            
                    
                    Django入门与实践-第23章：分页实现
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.25" data-path="ClassBasedViews2.html">
            
                <a href="ClassBasedViews2.html">
            
                    
                    Django入门与实践-第24章：我的账户
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.26" data-path="ClassBasedViews3.html">
            
                <a href="ClassBasedViews3.html">
            
                    
                    Django入门与实践-第25章：Markdown支持
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.27" data-path="ClassBasedViews4.html">
            
                <a href="ClassBasedViews4.html">
            
                    
                    Django入门与实践-第26章：个性化设置
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.28" data-path="Deployment.html">
            
                <a href="Deployment.html">
            
                    
                    Django入门与实践-第27章：项目部署
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Django入门与实践-第13章：表单处理</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="django&#x5165;&#x95E8;&#x4E0E;&#x5B9E;&#x8DF5;-&#x7B2C;13&#x7AE0;&#xFF1A;&#x8868;&#x5355;&#x5904;&#x7406;">Django&#x5165;&#x95E8;&#x4E0E;&#x5B9E;&#x8DF5;-&#x7B2C;13&#x7AE0;&#xFF1A;&#x8868;&#x5355;&#x5904;&#x7406;</h1>
<p>Forms(&#x8868;&#x5355;) &#x7528;&#x6765;&#x5904;&#x7406;&#x6211;&#x4EEC;&#x7684;&#x8F93;&#x5165;&#x3002;&#x8FD9;&#x5728;&#x4EFB;&#x4F55; web &#x5E94;&#x7528;&#x6216;&#x8005;&#x7F51;&#x7AD9;&#x4E2D;&#x90FD;&#x662F;&#x5F88;&#x5E38;&#x89C1;&#x7684;&#x4EFB;&#x52A1;&#x3002;&#x6807;&#x51C6;&#x7684;&#x505A;&#x6CD5;&#x662F;&#x901A;&#x8FC7; HTML &#x8868;&#x5355;&#x5B9E;&#x73B0;&#xFF0C;&#x7528;&#x6237;&#x8F93;&#x5165;&#x4E00;&#x4E9B;&#x6570;&#x636E;&#xFF0C;&#x5C06;&#x5176;&#x63D0;&#x4EA4;&#x7ED9;&#x670D;&#x52A1;&#x5668;&#xFF0C;&#x7136;&#x540E;&#x670D;&#x52A1;&#x5668;&#x5904;&#x7406;&#x5B83;&#x3002;</p>
<p><img src="statics/3-15.png" alt="3-15.png"></p>
<p>&#x8868;&#x5355;&#x5904;&#x7406;&#x662F;&#x4E00;&#x9879;&#x975E;&#x5E38;&#x590D;&#x6742;&#x7684;&#x4EFB;&#x52A1;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x6D89;&#x53CA;&#x5230;&#x4E0E;&#x5E94;&#x7528;&#x591A;&#x4E2A;&#x5C42;&#x9762;&#x7684;&#x4EA4;&#x4E92;&#x3002;&#x6709;&#x5F88;&#x591A;&#x9700;&#x8981;&#x5173;&#x5FC3;&#x7684;&#x95EE;&#x9898;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x63D0;&#x4EA4;&#x7ED9;&#x670D;&#x52A1;&#x5668;&#x7684;&#x6240;&#x6709;&#x6570;&#x636E;&#x90FD;&#x662F;&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x5F62;&#x5F0F;&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x5B83;&#x4E4B;&#x524D;&#x9700;&#x8981;&#x5C06;&#x5176;&#x8F6C;&#x6362;&#x4E3A;&#x9700;&#x8981;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;(&#x6574;&#x5F62;&#xFF0C;&#x6D6E;&#x70B9;&#x578B;&#xFF0C;&#x65E5;&#x671F;&#x7B49;)&#x3002;&#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x9A8C;&#x8BC1;&#x6709;&#x5173;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4E1A;&#x52A1;&#x903B;&#x8F91;&#x7684;&#x6570;&#x636E;&#x3002;&#x6211;&#x4EEC;&#x8FD8;&#x9700;&#x8981;&#x59A5;&#x5584;&#x5730;&#x6E05;&#x7406;&#x548C;&#x5BA1;&#x67E5;&#x6570;&#x636E;&#xFF0C;&#x4EE5;&#x907F;&#x514D;&#x4E00;&#x4E9B;&#x8BF8;&#x5982; SQL &#x6CE8;&#x5165;&#x548C; XSS &#x653B;&#x51FB;&#x7B49;&#x5B89;&#x5168;&#x95EE;&#x9898;&#x3002;</p>
<p>&#x597D;&#x6D88;&#x606F;&#x662F;&#xFF0C;Django Forms API &#x4F7F;&#x6574;&#x4E2A;&#x8FC7;&#x7A0B;&#x53D8;&#x7684;&#x66F4;&#x52A0;&#x7B80;&#x5355;&#xFF0C;&#x4ECE;&#x800C;&#x5B9E;&#x73B0;&#x4E86;&#x5927;&#x91CF;&#x5DE5;&#x4F5C;&#x7684;&#x81EA;&#x52A8;&#x5316;&#x3002;&#x800C;&#x4E14;&#xFF0C;&#x6700;&#x7EC8;&#x7684;&#x7ED3;&#x679C;&#x6BD4;&#x5927;&#x591A;&#x6570;&#x7A0B;&#x5E8F;&#x5458;&#x81EA;&#x5DF1;&#x53BB;&#x5B9E;&#x73B0;&#x7684;&#x4EE3;&#x7801;&#x66F4;&#x52A0;&#x5B89;&#x5168;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x4E0D;&#x7BA1; HTML &#x7684;&#x8868;&#x5355;&#x591A;&#x4E48;&#x7B80;&#x5355;&#xFF0C;&#x603B;&#x662F;&#x4F7F;&#x7528;Form API&#x3002;</p>
<h3 id="&#x81EA;&#x5DF1;&#x5B9E;&#x73B0;&#x8868;&#x5355;">&#x81EA;&#x5DF1;&#x5B9E;&#x73B0;&#x8868;&#x5355;</h3>
<p>&#x8D77;&#x521D;&#xFF0C;&#x6211;&#x60F3;&#x76F4;&#x63A5;&#x8DF3;&#x5230;&#x8868;&#x5355; API&#x3002;&#x4F46;&#x662F;&#x6211;&#x89C9;&#x5F97;&#x82B1;&#x70B9;&#x65F6;&#x95F4;&#x53BB;&#x4E86;&#x89E3;&#x4E00;&#x4E0B;&#x8868;&#x5355;&#x5904;&#x7406;&#x7684;&#x57FA;&#x672C;&#x7EC6;&#x8282;&#x662F;&#x4E00;&#x4E2A;&#x4E0D;&#x9519;&#x7684;&#x4E3B;&#x610F;&#x3002;&#x5426;&#x5219;&#xFF0C;&#x8FD9;&#x73A9;&#x610F;&#x513F;&#x5C06;&#x4F1A;&#x770B;&#x8D77;&#x6765;&#x50CF;&#x9B54;&#x672F;&#x4E00;&#x6837;&#xFF0C;&#x8FD9;&#x662F;&#x4E00;&#x4EF6;&#x574F;&#x4E8B;&#xFF0C;&#x56E0;&#x4E3A;&#x5F53;&#x51FA;&#x73B0;&#x9519;&#x8BEF;&#x65F6;&#xFF0C;&#x4F60;&#x5C06;&#x4E0D;&#x77E5;&#x9053;&#x600E;&#x4E48;&#x53BB;&#x627E;&#x5230;&#x95EE;&#x9898;&#x6240;&#x5728;&#x3002;</p>
<p>&#x968F;&#x7740;&#x5BF9;&#x4E00;&#x4E9B;&#x7F16;&#x7A0B;&#x6982;&#x5FF5;&#x7684;&#x6DF1;&#x5165;&#x7406;&#x89E3;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x611F;&#x89C9;&#x5230;&#x81EA;&#x5DF1;&#x80FD;&#x66F4;&#x597D;&#x5730;&#x638C;&#x63A7;&#x4E00;&#x4E9B;&#x60C5;&#x51B5;&#x3002;&#x638C;&#x63A7;&#x662F;&#x5F88;&#x91CD;&#x8981;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x5B83;&#x8BA9;&#x6211;&#x4EEC;&#x5199;&#x4EE3;&#x7801;&#x7684;&#x65F6;&#x5019;&#x66F4;&#x6709;&#x4FE1;&#x5FC3;&#x3002;&#x4E00;&#x65E6;&#x6211;&#x4EEC;&#x80FD;&#x786E;&#x5207;&#x5730;&#x77E5;&#x9053;&#x53D1;&#x751F;&#x4E86;&#x4EC0;&#x4E48;&#xFF0C;&#x5B9E;&#x73B0;&#x53EF;&#x9884;&#x89C1;&#x884C;&#x4E3A;&#x7684;&#x4EE3;&#x7801;&#x5C31;&#x5BB9;&#x6613;&#x591A;&#x4E86;&#x3002;&#x8C03;&#x8BD5;&#x548C;&#x67E5;&#x627E;&#x9519;&#x8BEF;&#x4E5F;&#x53D8;&#x5F97;&#x5F88;&#x5BB9;&#x6613;&#xFF0C;&#x56E0;&#x4E3A;&#x4F60;&#x77E5;&#x9053;&#x53BB;&#x54EA;&#x91CC;&#x67E5;&#x627E;&#x3002;</p>
<p>&#x65E0;&#x8BBA;&#x5982;&#x4F55;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x5F00;&#x59CB;&#x5B9E;&#x73B0;&#x4E0B;&#x9762;&#x7684;&#x8868;&#x5355;&#xFF1A;</p>
<p><img src="statics/3-16.png" alt="3-16.png"></p>
<p>&#x8FD9;&#x662F;&#x6211;&#x4EEC;&#x5728;&#x524D;&#x4E00;&#x4E2A;&#x6559;&#x7A0B;&#x7ED8;&#x5236;&#x7684;&#x7EBF;&#x6846;&#x56FE;&#x3002;&#x6211;&#x73B0;&#x5728;&#x610F;&#x8BC6;&#x5230;&#x8FD9;&#x4E2A;&#x53EF;&#x80FD;&#x662F;&#x4E00;&#x4E2A;&#x4E0D;&#x597D;&#x7684;&#x4F8B;&#x5B50;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x4E2A;&#x7279;&#x6B8A;&#x7684;&#x8868;&#x5355;&#x6D89;&#x53CA;&#x5230;&#x5904;&#x7406;&#x4E24;&#x4E2A;&#x4E0D;&#x540C;&#x6A21;&#x578B;&#x6570;&#x636E;&#xFF1A;<strong>Topic</strong>(subject) &#x548C; <strong>Post</strong>(message)&#x3002;</p>
<p>&#x8FD8;&#x6709;&#x4E00;&#x70B9;&#x5F88;&#x91CD;&#x8981;&#x7684;&#x6211;&#x4EEC;&#x5230;&#x73B0;&#x5728;&#x4E3A;&#x6B62;&#x8FD8;&#x6CA1;&#x8BA8;&#x8BBA;&#x8FC7;&#xFF0C;&#x5C31;&#x662F;&#x7528;&#x6237;&#x8BA4;&#x8BC1;&#x3002;&#x6211;&#x4EEC;&#x5E94;&#x8BE5;&#x53EA;&#x4E3A;&#x767B;&#x5F55;&#x8BA4;&#x8BC1;&#x8FC7;&#x7684;&#x7528;&#x6237;&#x53BB;&#x663E;&#x793A;&#x8FD9;&#x4E2A;&#x9875;&#x9762;&#x3002;&#x901A;&#x8FC7;&#x8FD9;&#x79CD;&#x65B9;&#x5F0F;&#xFF0C;&#x6211;&#x4EEC;&#x624D;&#x80FD;&#x77E5;&#x9053;&#x662F;&#x8C01;&#x521B;&#x5EFA;&#x4E86; <strong>Topic</strong> &#x6216;&#x8005; <strong>Post</strong>&#x3002;</p>
<p>&#x73B0;&#x5728;&#x8BA9;&#x6211;&#x4EEC;&#x62BD;&#x8C61;&#x4E00;&#x4E9B;&#x7EC6;&#x8282;&#xFF0C;&#x91CD;&#x70B9;&#x4E86;&#x89E3;&#x4E00;&#x4E0B;&#x600E;&#x4E48;&#x5728;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x4FDD;&#x5B58;&#x7528;&#x6237;&#x7684;&#x8F93;&#x5165;&#x3002;</p>
<p>&#x9996;&#x5148;&#xFF0C;&#x5148;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684; URL &#x8DEF;&#x7531;&#xFF0C;&#x547D;&#x540D;&#x4E3A; <strong>new_topic</strong>&#xFF1A;</p>
<p><strong>myproject/urls.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.conf.urls <span class="hljs-keyword">import</span> url
<span class="hljs-keyword">from</span> django.contrib <span class="hljs-keyword">import</span> admin

<span class="hljs-keyword">from</span> boards <span class="hljs-keyword">import</span> views

urlpatterns = [
    url(<span class="hljs-string">r&apos;^$&apos;</span>, views.home, name=<span class="hljs-string">&apos;home&apos;</span>),
    url(<span class="hljs-string">r&apos;^boards/(?P&lt;pk&gt;\d+)/$&apos;</span>, views.board_topics, name=<span class="hljs-string">&apos;board_topics&apos;</span>),
    url(<span class="hljs-string">r&apos;^boards/(?P&lt;pk&gt;\d+)/new/$&apos;</span>, views.new_topic, name=<span class="hljs-string">&apos;new_topic&apos;</span>),
    url(<span class="hljs-string">r&apos;^admin/&apos;</span>, admin.site.urls),
]
</code></pre>
<p>&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x7684;&#x8FD9;&#x4E2A; URL &#x80FD;&#x5E2E;&#x6211;&#x4EEC;&#x6807;&#x8BC6;&#x6B63;&#x786E;&#x7684; <strong>Board</strong></p>
<p>&#x73B0;&#x5728;&#x6765;&#x521B;&#x5EFA; <strong>new_topic</strong> &#x7684; &#x89C6;&#x56FE;&#x51FD;&#x6570;&#xFF1A;</p>
<p><strong>boards/views.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render, get_object_or_404
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Board

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">new_topic</span><span class="hljs-params">(request, pk)</span>:</span>
    board = get_object_or_404(Board, pk=pk)
    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">&apos;new_topic.html&apos;</span>, {<span class="hljs-string">&apos;board&apos;</span>: board})
</code></pre>
<p>&#x76EE;&#x524D;&#x4E3A;&#x6B62;&#xFF0C; <strong>new_topic</strong> &#x7684;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x770B;&#x8D77;&#x6765;&#x548C; <strong>board_topics</strong> &#x6070;&#x597D;&#x76F8;&#x540C;&#x3002;&#x8FD9;&#x662F;&#x6545;&#x610F;&#x7684;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x4E00;&#x6B65;&#x6B65;&#x5730;&#x6765;&#x3002;</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4E00;&#x4E2A;&#x540D;&#x4E3A; <strong>new_topic.html</strong> &#x7684;&#x6A21;&#x677F;&#xFF1A;</p>
<p><strong>templates/new_topic.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base.html&apos; %}

{% block title %}Start a New Topic{% endblock %}

{% block breadcrumb %}
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;home&apos; %}&quot;</span>&gt;</span>Boards<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;board_topics&apos; board.pk %}&quot;</span>&gt;</span>{{ board.name }}<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item active&quot;</span>&gt;</span>New topic<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
{% endblock %}

{% block content %}

{% endblock %}
</code></pre>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EA;&#x6709; breadcrumb &#x5BFC;&#x822A;&#x3002;&#x6CE8;&#x610F;&#x6211;&#x4EEC;&#x5305;&#x542B;&#x4E86;&#x8FD4;&#x56DE;&#x5230; <strong>board_topics</strong> &#x89C6;&#x56FE; URL&#x3002;</p>
<p>&#x6253;&#x5F00; URL <strong><a href="http://127.0.0.1:8000/boards/1/new/" target="_blank">http://127.0.0.1:8000/boards/1/new/</a></strong>&#x3002;&#x663E;&#x793A;&#x7ED3;&#x679C;&#x662F;&#x4E0B;&#x9762;&#x8FD9;&#x4E2A;&#x9875;&#x9762;&#xFF1A;</p>
<p>!(3-17.png)[./statics/3-17.png]</p>
<p>&#x6211;&#x4EEC;&#x4F9D;&#x7136;&#x8FD8;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;&#x5230;&#x8FBE;&#x8FD9;&#x4E2A;&#x65B0;&#x9875;&#x9762;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4F46;&#x662F;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x5C06; URL &#x6539;&#x4E3A; <strong><a href="http://127.0.0.1:8000/boards/2/new/" target="_blank">http://127.0.0.1:8000/boards/2/new/</a></strong>&#xFF0C;&#x5B83;&#x4F1A;&#x628A;&#x6211;&#x4EEC;&#x5E26;&#x5230; <strong>Python Board</strong> &#x7684;&#x9875;&#x9762;&#xFF1A;</p>
<p><img src="statics/3-18.png" alt="3-18.png"></p>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x5982;&#x679C;&#x4F60;&#x6CA1;&#x6709;&#x8DDF;&#x7740;&#x4E0A;&#x4E00;&#x8282;&#x8BFE;&#x7A0B;&#x4E00;&#x6B65;&#x6B65;&#x5730;&#x505A;&#xFF0C;&#x4F60;&#x7684;&#x7ED3;&#x679C;&#x548C;&#x6211;&#x7684;&#x53EF;&#x80FD;&#x6709;&#x4E9B;&#x4E0D;&#x4E00;&#x6837;&#x3002;&#x5728;&#x6211;&#x8FD9;&#x4E2A;&#x4F8B;&#x5B50;&#x4E2D;&#xFF0C;&#x6211;&#x7684;&#x6570;&#x636E;&#x5E93;&#x6709; 3 &#x4E2A; <strong>Board</strong> &#x5B9E;&#x4F8B;&#xFF0C;&#x5206;&#x522B;&#x662F; Django = 1, Python = 2, &#x548C; Random = 3&#x3002;&#x8FD9;&#x4E9B;&#x6570;&#x5B57;&#x662F;&#x6570;&#x636E;&#x5E93;&#x4E2D;&#x7684; ID&#xFF0C;&#x7528;&#x6765;&#x627E;&#x5230;&#x6B63;&#x786E;&#x7684;&#x8D44;&#x6E90;&#x3002;
`</p>
</blockquote>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x589E;&#x52A0;&#x4E00;&#x4E9B;&#x6D4B;&#x8BD5;&#x4E86;&#xFF1A;</p>
<p><strong>boards/tests.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.core.urlresolvers <span class="hljs-keyword">import</span> reverse
<span class="hljs-keyword">from</span> django.urls <span class="hljs-keyword">import</span> resolve
<span class="hljs-keyword">from</span> django.test <span class="hljs-keyword">import</span> TestCase
<span class="hljs-keyword">from</span> .views <span class="hljs-keyword">import</span> home, board_topics, new_topic
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Board

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HomeTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-comment"># ...</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BoardTopicsTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-comment"># ...</span>

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NewTopicTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setUp</span><span class="hljs-params">(self)</span>:</span>
        Board.objects.create(name=<span class="hljs-string">&apos;Django&apos;</span>, description=<span class="hljs-string">&apos;Django board.&apos;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_new_topic_view_success_status_code</span><span class="hljs-params">(self)</span>:</span>
        url = reverse(<span class="hljs-string">&apos;new_topic&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">1</span>})
        response = self.client.get(url)
        self.assertEquals(response.status_code, <span class="hljs-number">200</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_new_topic_view_not_found_status_code</span><span class="hljs-params">(self)</span>:</span>
        url = reverse(<span class="hljs-string">&apos;new_topic&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">99</span>})
        response = self.client.get(url)
        self.assertEquals(response.status_code, <span class="hljs-number">404</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_new_topic_url_resolves_new_topic_view</span><span class="hljs-params">(self)</span>:</span>
        view = resolve(<span class="hljs-string">&apos;/boards/1/new/&apos;</span>)
        self.assertEquals(view.func, new_topic)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_new_topic_view_contains_link_back_to_board_topics_view</span><span class="hljs-params">(self)</span>:</span>
        new_topic_url = reverse(<span class="hljs-string">&apos;new_topic&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">1</span>})
        board_topics_url = reverse(<span class="hljs-string">&apos;board_topics&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">1</span>})
        response = self.client.get(new_topic_url)
        self.assertContains(response, <span class="hljs-string">&apos;href=&quot;{0}&quot;&apos;</span>.format(board_topics_url))
</code></pre>
<p>&#x5173;&#x4E8E;&#x6211;&#x4EEC;&#x7684;&#x6D4B;&#x8BD5;&#x4E2D;&#x65B0;&#x7684; NewTopicTests &#x7C7B;&#x7684;&#x5FEB;&#x901F;&#x603B;&#x7ED3;&#xFF1A;</p>
<ul>
<li><strong>setUp</strong>&#xFF1A;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x4E2D;&#x4F7F;&#x7528;&#x7684; <strong>Board</strong> &#x5B9E;&#x4F8B;</li>
<li><strong>test_new_topic_view_success_status_cod</strong>&#xFF1A;&#x68C0;&#x67E5;&#x53D1;&#x7ED9; view &#x7684;&#x8BF7;&#x6C42;&#x662F;&#x5426;&#x6210;&#x529F;</li>
<li><strong>test_new_topic_view_not_found_status_code</strong>&#xFF1A;&#x68C0;&#x67E5;&#x5F53; <strong>Board</strong> &#x4E0D;&#x5B58;&#x5728;&#x65F6; view &#x662F;&#x5426;&#x4F1A;&#x629B;&#x51FA;&#x4E00;&#x4E2A; 404 &#x7684;&#x9519;&#x8BEF;</li>
<li><strong>test_new_topic_url_resolves_new_topic_view</strong>&#xFF1A;&#x68C0;&#x67E5;&#x662F;&#x5426;&#x6B63;&#x5728;&#x4F7F;&#x7528;&#x6B63;&#x786E;&#x7684; view</li>
<li><strong>test_new_topic_view_contains_link_back_to_board_topics_view</strong>&#xFF1A;&#x786E;&#x4FDD;&#x5BFC;&#x822A;&#x80FD;&#x56DE;&#x5230; topics &#x7684;&#x5217;&#x8868;</li>
</ul>
<p>&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<pre><code class="lang-shell">python manage.py test
</code></pre>
<pre><code class="lang-shell">Creating test database for alias &apos;default&apos;...
System check identified no issues (0 silenced).
...........
----------------------------------------------------------------------
Ran 11 tests in 0.076s

OK
Destroying test database for alias &apos;default&apos;...
</code></pre>
<p>&#x6210;&#x529F;&#xFF0C;&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x53BB;&#x5F00;&#x59CB;&#x521B;&#x5EFA;&#x8868;&#x5355;&#x4E86;&#x3002;</p>
<p><strong>templates/new_topic.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base.html&apos; %}

{% block title %}Start a New Topic{% endblock %}

{% block breadcrumb %}
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;home&apos; %}&quot;</span>&gt;</span>Boards<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;board_topics&apos; board.pk %}&quot;</span>&gt;</span>{{ board.name }}<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item active&quot;</span>&gt;</span>New topic<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
{% endblock %}

{% block content %}
  <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span>&gt;</span>
    {% csrf_token %}
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-group&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;id_subject&quot;</span>&gt;</span>Subject<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-control&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;id_subject&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;subject&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-group&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">label</span> <span class="hljs-attr">for</span>=<span class="hljs-string">&quot;id_message&quot;</span>&gt;</span>Message<span class="hljs-tag">&lt;/<span class="hljs-name">label</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">textarea</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-control&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;id_message&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;message&quot;</span> <span class="hljs-attr">rows</span>=<span class="hljs-string">&quot;5&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">textarea</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-success&quot;</span>&gt;</span>Post<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
{% endblock %}
</code></pre>
<p>&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x4F7F;&#x7528; Bootstrap 4 &#x63D0;&#x4F9B;&#x7684; CSS &#x7C7B;&#x624B;&#x52A8;&#x521B;&#x5EFA;&#x7684; HTML &#x8868;&#x5355;&#x3002;&#x5B83;&#x770B;&#x8D77;&#x6765;&#x662F;&#x8FD9;&#x4E2A;&#x6837;&#x5B50;&#xFF1A;</p>
<p>![3-19.png][./statics/3-19.png]</p>
<p>&#x5728; <code>&lt;form&gt;</code> &#x6807;&#x7B7E;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86; <code>method</code> &#x5C5E;&#x6027;&#x3002;&#x5B83;&#x4F1A;&#x544A;&#x8BC9;&#x6D4F;&#x89C8;&#x5668;&#x6211;&#x4EEC;&#x60F3;&#x5982;&#x4F55;&#x4E0E;&#x670D;&#x52A1;&#x5668;&#x901A;&#x4FE1;&#x3002;HTTP &#x89C4;&#x8303;&#x5B9A;&#x4E49;&#x4E86;&#x51E0;&#x79CD; request methods(&#x8BF7;&#x6C42;&#x65B9;&#x6CD5;)&#x3002;&#x4F46;&#x662F;&#x5728;&#x5927;&#x90E8;&#x5206;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x4F7F;&#x7528; <strong>GET</strong> &#x548C; <strong>POST</strong> &#x4E24;&#x79CD; request(&#x8BF7;&#x6C42;)&#x7C7B;&#x578B;&#x3002;</p>
<p><strong>GET</strong> &#x53EF;&#x80FD;&#x662F;&#x6700;&#x5E38;&#x89C1;&#x7684;&#x8BF7;&#x6C42;&#x7C7B;&#x578B;&#x4E86;&#x3002;&#x5B83;&#x7528;&#x4E8E;&#x4ECE;&#x670D;&#x52A1;&#x5668;&#x8BF7;&#x6C42;&#x6570;&#x636E;&#x3002;&#x6BCF;&#x5F53;&#x4F60;&#x70B9;&#x51FB;&#x4E86;&#x4E00;&#x4E2A;&#x94FE;&#x63A5;&#x6216;&#x8005;&#x76F4;&#x63A5;&#x5728;&#x6D4F;&#x89C8;&#x5668;&#x4E2D;&#x8F93;&#x5165;&#x4E86;&#x4E00;&#x4E2A;&#x7F51;&#x5740;&#x65F6;&#xFF0C;&#x4F60;&#x5C31;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; <strong>GET</strong> &#x8BF7;&#x6C42;&#x3002;</p>
<p><strong>POST</strong> &#x7528;&#x4E8E;&#x5F53;&#x6211;&#x4EEC;&#x60F3;&#x66F4;&#x6539;&#x670D;&#x52A1;&#x5668;&#x4E0A;&#x7684;&#x6570;&#x636E;&#x7684;&#x65F6;&#x5019;&#x3002;&#x4E00;&#x822C;&#x6765;&#x8BF4;&#xFF0C;&#x6BCF;&#x6B21;&#x6211;&#x4EEC;&#x53D1;&#x9001;&#x6570;&#x636E;&#x7ED9;&#x670D;&#x52A1;&#x5668;&#x90FD;&#x4F1A;&#x5BFC;&#x81F4;&#x8D44;&#x6E90;&#x72B6;&#x6001;&#x7684;&#x53D8;&#x5316;&#xFF0C;&#x6211;&#x4EEC;&#x5E94;&#x8BE5;&#x4F7F;&#x7528; <strong>POST</strong> &#x8BF7;&#x6C42;&#x53D1;&#x9001;&#x6570;&#x636E;&#x3002;</p>
<p>Django &#x4F7F;&#x7528; <strong>CSRF Token</strong>(Cross-Site Request Forgery Token) &#x4FDD;&#x62A4;&#x6240;&#x6709;&#x7684; <strong>POST</strong> &#x8BF7;&#x6C42;&#x3002;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x907F;&#x514D;&#x5916;&#x90E8;&#x7AD9;&#x70B9;&#x6216;&#x8005;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x5411;&#x6211;&#x4EEC;&#x7684;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x63D0;&#x4EA4;&#x6570;&#x636E;&#x7684;&#x5B89;&#x5168;&#x63AA;&#x65BD;&#x3002;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x6BCF;&#x6B21;&#x63A5;&#x6536;&#x4E00;&#x4E2A; <strong>POST</strong> &#x65F6;&#xFF0C;&#x90FD;&#x4F1A;&#x5148;&#x68C0;&#x67E5; <strong>CSRF Token</strong>&#x3002;&#x5982;&#x679C;&#x8FD9;&#x4E2A; request &#x6CA1;&#x6709; token&#xFF0C;&#x6216;&#x8005;&#x8FD9;&#x4E2A; token&#x662F;&#x65E0;&#x6548;&#x7684;&#xFF0C;&#x5B83;&#x5C31;&#x4F1A;&#x629B;&#x5F03;&#x63D0;&#x4EA4;&#x7684;&#x6570;&#x636E;&#x3002;</p>
<p><strong>csrf_token</strong> &#x7684;&#x6A21;&#x677F;&#x6807;&#x7B7E;&#xFF1A;</p>
<pre><code class="lang-html">{% csrf_token %}
</code></pre>
<p>&#x5B83;&#x662F;&#x4E0E;&#x5176;&#x4ED6;&#x8868;&#x5355;&#x6570;&#x636E;&#x4E00;&#x8D77;&#x63D0;&#x4EA4;&#x7684;&#x9690;&#x85CF;&#x5B57;&#x6BB5;&#xFF1A;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;hidden&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;csrfmiddlewaretoken&quot;</span> <span class="hljs-attr">value</span>=<span class="hljs-string">&quot;jG2o6aWj65YGaqzCpl0TYTg5jn6SctjzRZ9KmluifVx0IVaxlwh97YarZKs54Y32&quot;</span>&gt;</span>
</code></pre>
<p>&#x53E6;&#x5916;&#x4E00;&#x4EF6;&#x4E8B;&#x662F;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x8BBE;&#x7F6E; HTML &#x8F93;&#x5165;&#x7684; <strong>name</strong>&#xFF0C;<strong>name</strong> &#x5C06;&#x88AB;&#x7528;&#x6765;&#x5728;&#x670D;&#x52A1;&#x5668;&#x83B7;&#x53D6;&#x6570;&#x636E;&#x3002;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">input</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;text&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-control&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;id_subject&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;subject&quot;</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-name">textarea</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-control&quot;</span> <span class="hljs-attr">id</span>=<span class="hljs-string">&quot;id_message&quot;</span> <span class="hljs-attr">name</span>=<span class="hljs-string">&quot;message&quot;</span> <span class="hljs-attr">rows</span>=<span class="hljs-string">&quot;5&quot;</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">textarea</span>&gt;</span>
</code></pre>
<p>&#x4E0B;&#x9762;&#x662F;&#x793A;&#x8303;&#x6211;&#x4EEC;&#x5982;&#x4F55;&#x68C0;&#x7D22;&#x6570;&#x636E;&#xFF1A;</p>
<pre><code class="lang-python">subject = request.POST[<span class="hljs-string">&apos;subject&apos;</span>]
message = request.POST[<span class="hljs-string">&apos;message&apos;</span>]
</code></pre>
<p>&#x6240;&#x4EE5;&#xFF0C;&#x4ECE; HTML &#x83B7;&#x53D6;&#x6570;&#x636E;&#x5E76;&#x4E14;&#x5F00;&#x59CB;&#x4E00;&#x4E2A;&#x65B0;&#x7684; topic &#x89C6;&#x56FE;&#x7684;&#x7B80;&#x5355;&#x5B9E;&#x73B0;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x5199;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.contrib.auth.models <span class="hljs-keyword">import</span> User
<span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render, redirect, get_object_or_404
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Board, Topic, Post

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">new_topic</span><span class="hljs-params">(request, pk)</span>:</span>
    board = get_object_or_404(Board, pk=pk)

    <span class="hljs-keyword">if</span> request.method == <span class="hljs-string">&apos;POST&apos;</span>:
        subject = request.POST[<span class="hljs-string">&apos;subject&apos;</span>]
        message = request.POST[<span class="hljs-string">&apos;message&apos;</span>]

        user = User.objects.first()  <span class="hljs-comment"># <span class="hljs-doctag">TODO:</span> &#x4E34;&#x65F6;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x8D26;&#x53F7;&#x4F5C;&#x4E3A;&#x767B;&#x5F55;&#x7528;&#x6237;</span>

        topic = Topic.objects.create(
            subject=subject,
            board=board,
            starter=user
        )

        post = Post.objects.create(
            message=message,
            topic=topic,
            created_by=user
        )

        <span class="hljs-keyword">return</span> redirect(<span class="hljs-string">&apos;board_topics&apos;</span>, pk=board.pk)  <span class="hljs-comment"># <span class="hljs-doctag">TODO:</span> redirect to the created topic page</span>

    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">&apos;new_topic.html&apos;</span>, {<span class="hljs-string">&apos;board&apos;</span>: board})
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x53EA;&#x8003;&#x8651;&#x80FD;&#x63A5;&#x6536;&#x6570;&#x636E;&#x5E76;&#x4E14;&#x4FDD;&#x5B58;&#x8FDB;&#x6570;&#x636E;&#x5E93;&#x7684;&#x4E50;&#x89C2;&#x5408;&#x6CD5;&#x7684; path&#xFF0C;&#x4F46;&#x662F;&#x8FD8;&#x7F3A;&#x5C11;&#x4E00;&#x4E9B;&#x90E8;&#x5206;&#x3002;&#x6211;&#x4EEC;&#x6CA1;&#x6709;&#x9A8C;&#x8BC1;&#x6570;&#x636E;&#x3002;&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x63D0;&#x4EA4;&#x7A7A;&#x8868;&#x5355;&#x6216;&#x8005;&#x63D0;&#x4EA4;&#x4E00;&#x4E2A;&#x5927;&#x4E8E; 255 &#x4E2A;&#x5B57;&#x7B26;&#x7684; <strong>subject</strong>&#x3002;</p>
<p>&#x5230;&#x76EE;&#x524D;&#x4E3A;&#x6B62;&#x6211;&#x4EEC;&#x90FD;&#x5728;&#x5BF9; <strong>User</strong> &#x5B57;&#x6BB5;&#x8FDB;&#x884C;&#x786C;&#x7F16;&#x7801;&#xFF0C;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x8FD8;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;&#x8EAB;&#x4EFD;&#x9A8C;&#x8BC1;&#x3002;&#x6709;&#x4E00;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x65B9;&#x6CD5;&#x6765;&#x8BC6;&#x522B;&#x767B;&#x5F55;&#x7684;&#x7528;&#x6237;&#x3002;&#x6211;&#x4EEC;&#x4F1A;&#x5728;&#x4E0B;&#x4E00;&#x4E2A;&#x8BFE;&#x7A0B;&#x5C06;&#x8FD9;&#x4E00;&#x5757;&#x3002;&#x6B64;&#x5916;&#xFF0C;&#x6211;&#x4EEC;&#x8FD8;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;&#x5217;&#x51FA; topic &#x7684;&#x6240;&#x6709; posts &#x7684;&#x89C6;&#x56FE;&#xFF0C;&#x5B9E;&#x73B0;&#x4E86;&#x5B83;&#xFF0C;&#x6211;&#x4EEC;&#x5C31;&#x53EF;&#x4EE5;&#x5C06;&#x7528;&#x6237;&#x91CD;&#x5B9A;&#x5411;&#x5230;&#x5217;&#x51FA;&#x6240;&#x6709;&#x4E3B;&#x9898;&#x7684;&#x5217;&#x8868;&#x9875;&#x9762;&#x3002;</p>
<p><img src="statics/3-20.png" alt="3-20.png"></p>
<p>&#x70B9;&#x51FB; <strong>Post</strong> &#x6309;&#x94AE;&#x63D0;&#x4EA4;&#x8868;&#x5355;&#xFF1A;</p>
<p><img src="statics/3-21.png" alt="3-21.png"></p>
<p>&#x770B;&#x8D77;&#x6765;&#x6210;&#x529F;&#x4E86;&#x3002;&#x4F46;&#x662F;&#x6211;&#x4EEC;&#x8FD8;&#x6CA1;&#x6709;&#x5B9E;&#x73B0;&#x4E3B;&#x9898;&#x7684;&#x5217;&#x8868;&#x9875;&#x9762;&#xFF0C;&#x6240;&#x4EE5;&#x6CA1;&#x6709;&#x4E1C;&#x897F;&#x53EF;&#x4EE5;&#x770B;&#x3002;&#x8BA9;&#x6211;&#x4EEC;&#x6765;&#x7F16;&#x8F91; <strong>templates/topics.html</strong> &#x6765;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x5408;&#x9002;&#x7684;&#x5217;&#x8868;&#xFF1A;</p>
<p><strong>templates/topics.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base.html&apos; %}

{% block title %}
  {{ board.name }} - {{ block.super }}
{% endblock %}

{% block breadcrumb %}
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;home&apos; %}&quot;</span>&gt;</span>Boards<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item active&quot;</span>&gt;</span>{{ board.name }}<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
{% endblock %}

{% block content %}
  <span class="hljs-tag">&lt;<span class="hljs-name">table</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;table&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">thead</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;thead-inverse&quot;</span>&gt;</span>
      <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>Topic<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>Starter<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>Replies<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>Views<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span>
        <span class="hljs-tag">&lt;<span class="hljs-name">th</span>&gt;</span>Last Update<span class="hljs-tag">&lt;/<span class="hljs-name">th</span>&gt;</span>
      <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
    <span class="hljs-tag">&lt;/<span class="hljs-name">thead</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">tbody</span>&gt;</span>
      {% for topic in board.topics.all %}
        <span class="hljs-tag">&lt;<span class="hljs-name">tr</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>{{ topic.subject }}<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>{{ topic.starter.username }}<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>0<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>0<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
          <span class="hljs-tag">&lt;<span class="hljs-name">td</span>&gt;</span>{{ topic.last_updated }}<span class="hljs-tag">&lt;/<span class="hljs-name">td</span>&gt;</span>
        <span class="hljs-tag">&lt;/<span class="hljs-name">tr</span>&gt;</span>
      {% endfor %}
    <span class="hljs-tag">&lt;/<span class="hljs-name">tbody</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span>
{% endblock %}
</code></pre>
<p><img src="statics/3-22.png" alt="3-22.png"></p>
<p>&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x7684; <strong>Topic</strong> &#x663E;&#x793A;&#x5728;&#x8FD9;&#x4E0A;&#x9762;&#x4E86;&#x3002;</p>
<p>&#x8FD9;&#x91CC;&#x6709;&#x4E24;&#x4E2A;&#x65B0;&#x6982;&#x5FF5;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x9996;&#x6B21;&#x4F7F;&#x7528; <strong>Board</strong> &#x6A21;&#x578B;&#x4E2D;&#x7684; <strong>topics</strong> &#x5C5E;&#x6027;&#x3002;<strong>topics</strong> &#x5C5E;&#x6027;&#x7531; Django &#x4F7F;&#x7528;&#x53CD;&#x5411;&#x5173;&#x7CFB;&#x81EA;&#x52A8;&#x521B;&#x5EFA;&#x3002;&#x5728;&#x4E4B;&#x524D;&#x7684;&#x6B65;&#x9AA4;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E86;&#x4E00;&#x4E2A; <strong>Topic</strong> &#x5B9E;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">new_topic</span><span class="hljs-params">(request, pk)</span>:</span>
    board = get_object_or_404(Board, pk=pk)

    <span class="hljs-comment"># ...</span>

    topic = Topic.objects.create(
        subject=subject,
        board=board,
        starter=user
    )
</code></pre>
<p>&#x5728; <code>board=board</code> &#x8FD9;&#x884C;&#xFF0C;&#x6211;&#x4EEC;&#x8BBE;&#x7F6E;&#x4E86; <strong>Topic</strong> &#x6A21;&#x578B;&#x4E2D;&#x7684; board &#x5B57;&#x6BB5;&#xFF0C;&#x5B83;&#x662F; <code>ForeignKey(Board)</code>&#x3002;&#x56E0;&#x6B64;&#xFF0C;&#x6211;&#x4EEC;&#x7684; <strong>Board</strong> &#x5B9E;&#x4F8B;&#x5C31;&#x77E5;&#x9053;&#x4E86;&#x4E0E;&#x5B83;&#x5173;&#x8054;&#x7684; <strong>Topic</strong> &#x5B9E;&#x4F8B;&#x3002;</p>
<p>&#x4E4B;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x4F7F;&#x7528; <code>board.topics.all</code> &#x800C;&#x4E0D;&#x662F; <code>board.topics</code>&#xFF0C;&#x662F;&#x56E0;&#x4E3A; <code>board.topics</code> &#x662F;&#x4E00;&#x4E2A; <strong>Related Manager</strong>,&#x5B83;&#x4E0E; <strong>Model Manager</strong> &#x5F88;&#x76F8;&#x4F3C;&#xFF0C;&#x901A;&#x5E38;&#x5728; <code>board.objects</code> &#x53EF;&#x5F97;&#x5230;&#x3002;&#x6240;&#x4EE5;&#xFF0C;&#x8981;&#x8FD4;&#x56DE;&#x7ED9;&#x5B9A; board &#x7684;&#x6240;&#x6709; topic &#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x4F7F;&#x7528; <code>board.topics.all()</code>&#xFF0C;&#x8981;&#x8FC7;&#x6EE4;&#x4E00;&#x4E9B;&#x6570;&#x636E;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x8FD9;&#x6837;&#x7528; <code>board.topics.filter(subject__contains=&apos;Hello&apos;)</code>&#x3002;</p>
<p>&#x53E6;&#x4E00;&#x4E2A;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x662F;&#xFF0C;&#x5728; Python &#x4EE3;&#x7801;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x5FC5;&#x987B;&#x4F7F;&#x7528;&#x62EC;&#x53F7;&#xFF1A;<code>board.topics.all()</code>&#xFF0C;&#x56E0;&#x4E3A; <code>all()</code> &#x662F;&#x4E00;&#x4E2A;&#x65B9;&#x6CD5;&#x3002;&#x5728;&#x4F7F;&#x7528; Django &#x6A21;&#x677F;&#x8BED;&#x8A00;&#x5199;&#x4EE3;&#x7801;&#x7684;&#x65F6;&#x5019;&#xFF0C;&#x5728;&#x4E00;&#x4E2A; HTML &#x6A21;&#x677F;&#x6587;&#x4EF6;&#x91CC;&#x9762;&#xFF0C;&#x6211;&#x4EEC;&#x4E0D;&#x4F7F;&#x7528;&#x62EC;&#x53F7;&#xFF0C;&#x5C31;&#x53EA;&#x662F; <code>board.topics.all</code>&#x3002;</p>
<p>&#x7B2C;&#x4E8C;&#x4EF6;&#x4E8B;&#x662F;&#x6211;&#x4EEC;&#x5728;&#x4F7F;&#x7528; <code>ForeignKey</code>&#xFF1A;</p>
<pre><code class="lang-html">{{ topic.starter.username }}
</code></pre>
<p>&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x70B9;&#x52A0;&#x4E0A;&#x5C5E;&#x6027;&#x8FD9;&#x79CD;&#x5199;&#x6CD5;&#xFF0C;&#x6211;&#x4EEC;&#x51E0;&#x4E4E;&#x53EF;&#x4EE5;&#x8BBF;&#x95EE; <strong>User</strong> &#x6A21;&#x578B;&#x7684;&#x6240;&#x6709;&#x5C5E;&#x6027;&#x3002;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x60F3;&#x5F97;&#x5230;&#x7528;&#x6237;&#x7684; email&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>topic.starter.email</code>&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x4FEE;&#x6539;&#x4E86; <strong>topics.html</strong> &#x6A21;&#x677F;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x80FD;&#x8BA9;&#x6211;&#x4EEC;&#x8F6C;&#x5230; <strong>new topic</strong> &#x9875;&#x9762;&#x7684;&#x6309;&#x94AE;&#xFF1A;</p>
<p><strong>templates/topics.html</strong></p>
<pre><code class="lang-html">{% block content %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;mb-4&quot;</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;new_topic&apos; board.pk %}&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-primary&quot;</span>&gt;</span>New topic<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>

  <span class="hljs-tag">&lt;<span class="hljs-name">table</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;table&quot;</span>&gt;</span>
    <span class="hljs-comment">&lt;!-- code suppressed for brevity --&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">table</span>&gt;</span>
{% endblock %}
</code></pre>
<p><img src="statics/3-23.png" alt="3-23.png"></p>
<p>&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5199;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#x4EE5;&#x786E;&#x4FDD;&#x7528;&#x6237;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x6B64;&#x9875;&#x9762;&#x8BBF;&#x95EE;&#x5230; <strong>New Topic</strong> &#x9875;&#x9762;&#xFF1A;</p>
<p><strong>boards/tests.py</strong></p>
<pre><code class="lang-python"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">BoardTopicsTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-comment"># ...</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_board_topics_view_contains_navigation_links</span><span class="hljs-params">(self)</span>:</span>
        board_topics_url = reverse(<span class="hljs-string">&apos;board_topics&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">1</span>})
        homepage_url = reverse(<span class="hljs-string">&apos;home&apos;</span>)
        new_topic_url = reverse(<span class="hljs-string">&apos;new_topic&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">1</span>})

        response = self.client.get(board_topics_url)

        self.assertContains(response, <span class="hljs-string">&apos;href=&quot;{0}&quot;&apos;</span>.format(homepage_url))
        self.assertContains(response, <span class="hljs-string">&apos;href=&quot;{0}&quot;&apos;</span>.format(new_topic_url))
</code></pre>
<p>&#x6211;&#x5728;&#x8FD9;&#x91CC;&#x57FA;&#x672C;&#x4E0A;&#x91CD;&#x547D;&#x540D;&#x4E86; <strong>test_board_topics_view_contains_link_back_to_homepage</strong> &#x65B9;&#x6CD5;&#x5E76;&#x6DFB;&#x52A0;&#x4E86;&#x4E00;&#x4E2A;&#x989D;&#x5916;&#x7684; <code>assertContains</code>&#x3002;&#x8FD9;&#x4E2A;&#x6D4B;&#x8BD5;&#x73B0;&#x5728;&#x8D1F;&#x8D23;&#x786E;&#x4FDD;&#x6211;&#x4EEC;&#x7684; view &#x5305;&#x542B;&#x6240;&#x9700;&#x7684;&#x5BFC;&#x822A;&#x94FE;&#x63A5;&#x3002;</p>
<h3 id="&#x6D4B;&#x8BD5;&#x8868;&#x5355;">&#x6D4B;&#x8BD5;&#x8868;&#x5355;</h3>
<p>&#x5728;&#x6211;&#x4EEC;&#x4F7F;&#x7528; Django &#x7684;&#x65B9;&#x5F0F;&#x7F16;&#x5199;&#x4E4B;&#x524D;&#x7684;&#x8868;&#x5355;&#x793A;&#x4F8B;&#x4E4B;&#x524D;, &#x8BA9;&#x6211;&#x4EEC;&#x5148;&#x4E3A;&#x8868;&#x5355;&#x5904;&#x7406;&#x5199;&#x4E00;&#x4E9B;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<p><strong>boards/tests.py</strong></p>
<pre><code class="lang-python"><span class="hljs-string">&apos;&apos;&apos; new imports below &apos;&apos;&apos;</span>
<span class="hljs-keyword">from</span> django.contrib.auth.models <span class="hljs-keyword">import</span> User
<span class="hljs-keyword">from</span> .views <span class="hljs-keyword">import</span> new_topic
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Board, Topic, Post

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NewTopicTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">setUp</span><span class="hljs-params">(self)</span>:</span>
        Board.objects.create(name=<span class="hljs-string">&apos;Django&apos;</span>, description=<span class="hljs-string">&apos;Django board.&apos;</span>)
        User.objects.create_user(username=<span class="hljs-string">&apos;john&apos;</span>, email=<span class="hljs-string">&apos;john@doe.com&apos;</span>, password=<span class="hljs-string">&apos;123&apos;</span>)  <span class="hljs-comment"># &lt;- included this line here</span>

    <span class="hljs-comment"># ...</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_csrf</span><span class="hljs-params">(self)</span>:</span>
        url = reverse(<span class="hljs-string">&apos;new_topic&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">1</span>})
        response = self.client.get(url)
        self.assertContains(response, <span class="hljs-string">&apos;csrfmiddlewaretoken&apos;</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_new_topic_valid_post_data</span><span class="hljs-params">(self)</span>:</span>
        url = reverse(<span class="hljs-string">&apos;new_topic&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">1</span>})
        data = {
            <span class="hljs-string">&apos;subject&apos;</span>: <span class="hljs-string">&apos;Test title&apos;</span>,
            <span class="hljs-string">&apos;message&apos;</span>: <span class="hljs-string">&apos;Lorem ipsum dolor sit amet&apos;</span>
        }
        response = self.client.post(url, data)
        self.assertTrue(Topic.objects.exists())
        self.assertTrue(Post.objects.exists())

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_new_topic_invalid_post_data</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&apos;&apos;&apos;
        Invalid post data should not redirect
        The expected behavior is to show the form again with validation errors
        &apos;&apos;&apos;</span>
        url = reverse(<span class="hljs-string">&apos;new_topic&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">1</span>})
        response = self.client.post(url, {})
        self.assertEquals(response.status_code, <span class="hljs-number">200</span>)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_new_topic_invalid_post_data_empty_fields</span><span class="hljs-params">(self)</span>:</span>
        <span class="hljs-string">&apos;&apos;&apos;
        Invalid post data should not redirect
        The expected behavior is to show the form again with validation errors
        &apos;&apos;&apos;</span>
        url = reverse(<span class="hljs-string">&apos;new_topic&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">1</span>})
        data = {
            <span class="hljs-string">&apos;subject&apos;</span>: <span class="hljs-string">&apos;&apos;</span>,
            <span class="hljs-string">&apos;message&apos;</span>: <span class="hljs-string">&apos;&apos;</span>
        }
        response = self.client.post(url, data)
        self.assertEquals(response.status_code, <span class="hljs-number">200</span>)
        self.assertFalse(Topic.objects.exists())
        self.assertFalse(Post.objects.exists())
</code></pre>
<p>&#x9996;&#x5148;&#xFF0C; <strong>test.py</strong> &#x6587;&#x4EF6;&#x53D8;&#x7684;&#x8D8A;&#x6765;&#x8D8A;&#x5927;&#x3002;&#x6211;&#x4EEC;&#x4F1A;&#x5C3D;&#x5FEB;&#x6539;&#x8FDB;&#x5B83;&#xFF0C;&#x5C06;&#x6D4B;&#x8BD5;&#x5206;&#x4E3A;&#x51E0;&#x4E2A;&#x6587;&#x4EF6;&#x3002;&#x4F46;&#x73B0;&#x5728;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x5148;&#x4FDD;&#x6301;&#x8FD9;&#x4E2A;&#x72B6;&#x6001;&#x3002;</p>
<ul>
<li><strong>setUp</strong>&#xFF1A;&#x5305;&#x542B; <code>User.objects.create_user</code> &#x4EE5;&#x521B;&#x5EFA;&#x7528;&#x4E8E;&#x6D4B;&#x8BD5;&#x7684; <strong>User</strong> &#x5B9E;&#x4F8B;&#x3002;</li>
<li></li>
<li><strong>test_csrf</strong>&#xFF1A;&#x7531;&#x4E8E; <strong>CSRF Token</strong> &#x662F;&#x5904;&#x7406; <strong>Post</strong> &#x8BF7;&#x6C42;&#x7684;&#x57FA;&#x672C;&#x90E8;&#x5206;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4FDD;&#x8BC1;&#x6211;&#x4EEC;&#x7684; HTML &#x5305;&#x542B; token&#x3002;</li>
<li></li>
<li><strong>test_new_topic_valid_post_data</strong>&#xFF1A;&#x53D1;&#x9001;&#x6709;&#x6548;&#x7684;&#x6570;&#x636E;&#x5E76;&#x68C0;&#x67E5;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x662F;&#x5426;&#x521B;&#x5EFA;&#x4E86; <strong>Topic</strong> &#x548C; <strong>Post</strong> &#x5B9E;&#x4F8B;&#x3002;</li>
<li></li>
<li><strong>test_new_topic_invalid_post_data</strong>&#xFF1A;&#x53D1;&#x9001;&#x4E00;&#x4E2A;&#x7A7A;&#x5B57;&#x5178;&#x6765;&#x68C0;&#x67E5;&#x5E94;&#x7528;&#x7684;&#x884C;&#x4E3A;&#x3002;</li>
<li></li>
<li><strong>test_new_topic_invalid_post_data_empty_fields</strong>&#xFF1A;&#x7C7B;&#x4F3C;&#x4E8E;&#x4E0A;&#x4E00;&#x4E2A;&#x6D4B;&#x8BD5;&#xFF0C;&#x4F46;&#x662F;&#x8FD9;&#x6B21;&#x6211;&#x4EEC;&#x53D1;&#x9001;&#x4E00;&#x4E9B;&#x6570;&#x636E;&#x3002;&#x9884;&#x671F;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x4F1A;&#x9A8C;&#x8BC1;&#x5E76;&#x4E14;&#x62D2;&#x7EDD;&#x7A7A;&#x7684; subject &#x548C; message&#x3002;</li>
</ul>
<p>&#x8FD0;&#x884C;&#x8FD9;&#x4E9B;&#x6D4B;&#x8BD5;&#xFF1A;</p>
<pre><code class="lang-python">python manage.py test
</code></pre>
<pre><code class="lang-python">Creating test database <span class="hljs-keyword">for</span> alias <span class="hljs-string">&apos;default&apos;</span>...
System check identified no issues (<span class="hljs-number">0</span> silenced).
........EF.....
======================================================================
ERROR: test_new_topic_invalid_post_data (boards.tests.NewTopicTests)
----------------------------------------------------------------------
Traceback (most recent call last):
...
django.utils.datastructures.MultiValueDictKeyError: <span class="hljs-string">&quot;&apos;subject&apos;&quot;</span>

======================================================================
FAIL: test_new_topic_invalid_post_data_empty_fields (boards.tests.NewTopicTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File <span class="hljs-string">&quot;/Users/vitorfs/Development/myproject/django-beginners-guide/boards/tests.py&quot;</span>, line <span class="hljs-number">115</span>, <span class="hljs-keyword">in</span> test_new_topic_invalid_post_data_empty_fields
    self.assertEquals(response.status_code, <span class="hljs-number">200</span>)
AssertionError: <span class="hljs-number">302</span> != <span class="hljs-number">200</span>

----------------------------------------------------------------------
Ran <span class="hljs-number">15</span> tests <span class="hljs-keyword">in</span> <span class="hljs-number">0.512</span>s

FAILED (failures=<span class="hljs-number">1</span>, errors=<span class="hljs-number">1</span>)
Destroying test database <span class="hljs-keyword">for</span> alias <span class="hljs-string">&apos;default&apos;</span>...
</code></pre>
<p>&#x6709;&#x4E00;&#x4E2A;&#x5931;&#x8D25;&#x7684;&#x6D4B;&#x8BD5;&#x548C;&#x4E00;&#x4E2A;&#x9519;&#x8BEF;&#x3002;&#x4E24;&#x4E2A;&#x90FD;&#x4E0E;&#x9A8C;&#x8BC1;&#x7528;&#x6237;&#x7684;&#x8F93;&#x5165;&#x6709;&#x5173;&#x3002;&#x4E0D;&#x8981;&#x8BD5;&#x56FE;&#x7528;&#x5F53;&#x524D;&#x7684;&#x5B9E;&#x73B0;&#x6765;&#x4FEE;&#x590D;&#x5B83;&#xFF0C;&#x8BA9;&#x6211;&#x4EEC;&#x901A;&#x8FC7;&#x4F7F;&#x7528; Django Forms API &#x6765;&#x901A;&#x8FC7;&#x8FD9;&#x4E9B;&#x6D4B;&#x8BD5;</p>
<h3 id="&#x521B;&#x5EFA;&#x8868;&#x5355;&#x6B63;&#x786E;&#x7684;&#x59FF;&#x52BF;">&#x521B;&#x5EFA;&#x8868;&#x5355;&#x6B63;&#x786E;&#x7684;&#x59FF;&#x52BF;</h3>
<p>&#x81EA;&#x4ECE;&#x6211;&#x4EEC;&#x5F00;&#x59CB;&#x4F7F;&#x7528; Forms&#xFF0C;&#x6211;&#x4EEC;&#x5DF2;&#x7ECF;&#x8D70;&#x4E86;&#x5F88;&#x957F;&#x4E00;&#x6BB5;&#x8DEF;&#x3002;&#x7EC8;&#x4E8E;&#xFF0C;&#x662F;&#x65F6;&#x5019;&#x4F7F;&#x7528; Forms API &#x4E86;&#x3002;</p>
<p>Forms API &#x53EF;&#x5728;&#x6A21;&#x5757; <code>django.forms</code> &#x4E2D;&#x5F97;&#x5230;&#x3002;Django &#x4F7F;&#x7528;&#x4E24;&#x79CD;&#x7C7B;&#x578B;&#x7684; form&#xFF1A;<code>forms.Form</code> &#x548C; <code>forms.ModelForm</code>&#x3002;<code>Form</code> &#x7C7B;&#x662F;&#x901A;&#x7528;&#x7684;&#x8868;&#x5355;&#x5B9E;&#x73B0;&#x3002;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B83;&#x6765;&#x5904;&#x7406;&#x4E0E;&#x5E94;&#x7528;&#x7A0B;&#x5E8F; model &#x6CA1;&#x6709;&#x76F4;&#x63A5;&#x5173;&#x8054;&#x7684;&#x6570;&#x636E;&#x3002;<code>ModelForm</code> &#x662F; <code>Form</code> &#x7684;&#x5B50;&#x7C7B;&#xFF0C;&#x5B83;&#x4E0E; model &#x7C7B;&#x76F8;&#x5173;&#x8054;&#x3002;</p>
<p>&#x5728; <strong>boards</strong> &#x6587;&#x4EF6;&#x5939;&#x4E0B;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x6587;&#x4EF6; <code>forms.py</code>&#xFF1A;</p>
<p><strong>boards/forms.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django <span class="hljs-keyword">import</span> forms
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Topic

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NewTopicForm</span><span class="hljs-params">(forms.ModelForm)</span>:</span>
    message = forms.CharField(widget=forms.Textarea(), max_length=<span class="hljs-number">4000</span>)

    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        model = Topic
        fields = [<span class="hljs-string">&apos;subject&apos;</span>, <span class="hljs-string">&apos;message&apos;</span>]
</code></pre>
<p>&#x8FD9;&#x662F;&#x6211;&#x4EEC;&#x7684;&#x7B2C;&#x4E00;&#x4E2A; form&#x3002;&#x5B83;&#x662F;&#x4E00;&#x4E2A;&#x4E0E; <strong>Topic</strong> model &#x76F8;&#x5173;&#x8054;&#x7684; <code>ModelForm</code>&#x3002;<strong>Meta</strong> &#x7C7B;&#x91CC;&#x9762; <code>fields</code> &#x5217;&#x8868;&#x4E2D;&#x7684; <code>subject</code> &#x5F15;&#x7528; <strong>Topic</strong> &#x7C7B;&#x4E2D;&#x7684; <strong>subject</strong> field(&#x5B57;&#x6BB5;)&#x3002;&#x73B0;&#x5728;&#x6CE8;&#x610F;&#x5230;&#x6211;&#x4EEC;&#x5B9A;&#x4E49;&#x4E86;&#x4E00;&#x4E2A;&#x53EB;&#x505A; <code>message</code> &#x7684;&#x989D;&#x5916;&#x5B57;&#x6BB5;&#x3002;&#x5B83;&#x7528;&#x6765;&#x5F15;&#x7528; <strong>Post</strong> &#x4E2D;&#x6211;&#x4EEC;&#x60F3;&#x8981;&#x4FDD;&#x5B58;&#x7684; message&#x3002;</p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x91CD;&#x5199;&#x6211;&#x4EEC;&#x7684; <strong>views.py</strong>&#xFF1A;</p>
<p><strong>boards/views.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.contrib.auth.models <span class="hljs-keyword">import</span> User
<span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> render, redirect, get_object_or_404
<span class="hljs-keyword">from</span> .forms <span class="hljs-keyword">import</span> NewTopicForm
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Board, Topic, Post

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">new_topic</span><span class="hljs-params">(request, pk)</span>:</span>
    board = get_object_or_404(Board, pk=pk)
    user = User.objects.first()  <span class="hljs-comment"># <span class="hljs-doctag">TODO:</span> get the currently logged in user</span>
    <span class="hljs-keyword">if</span> request.method == <span class="hljs-string">&apos;POST&apos;</span>:
        form = NewTopicForm(request.POST)
        <span class="hljs-keyword">if</span> form.is_valid():
            topic = form.save(commit=<span class="hljs-keyword">False</span>)
            topic.board = board
            topic.starter = user
            topic.save()
            post = Post.objects.create(
                message=form.cleaned_data.get(<span class="hljs-string">&apos;message&apos;</span>),
                topic=topic,
                created_by=user
            )
            <span class="hljs-keyword">return</span> redirect(<span class="hljs-string">&apos;board_topics&apos;</span>, pk=board.pk)  <span class="hljs-comment"># <span class="hljs-doctag">TODO:</span> redirect to the created topic page</span>
    <span class="hljs-keyword">else</span>:
        form = NewTopicForm()
    <span class="hljs-keyword">return</span> render(request, <span class="hljs-string">&apos;new_topic.html&apos;</span>, {<span class="hljs-string">&apos;board&apos;</span>: board, <span class="hljs-string">&apos;form&apos;</span>: form})
</code></pre>
<p>&#x8FD9;&#x662F;&#x6211;&#x4EEC;&#x5728; view(&#x89C6;&#x56FE;) &#x4E2D;&#x5904;&#x7406; form(&#x8868;&#x5355;) &#x7684;&#x65B9;&#x5F0F;&#x3002;&#x8BA9;&#x6211;&#x4EEC;&#x53BB;&#x6389;&#x4E00;&#x4E9B;&#x591A;&#x4F59;&#x7684;&#x90E8;&#x5206;&#xFF0C;&#x53EA;&#x770B;&#x8868;&#x5355;&#x5904;&#x7406;&#x7684;&#x6838;&#x5FC3;&#x90E8;&#x5206;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">if</span> request.method == <span class="hljs-string">&apos;POST&apos;</span>:
    form = NewTopicForm(request.POST)
    <span class="hljs-keyword">if</span> form.is_valid():
        topic = form.save()
        <span class="hljs-keyword">return</span> redirect(<span class="hljs-string">&apos;board_topics&apos;</span>, pk=board.pk)
<span class="hljs-keyword">else</span>:
    form = NewTopicForm()
<span class="hljs-keyword">return</span> render(request, <span class="hljs-string">&apos;new_topic.html&apos;</span>, {<span class="hljs-string">&apos;form&apos;</span>: form})
</code></pre>
<p>&#x9996;&#x5148;&#x6211;&#x4EEC;&#x5224;&#x65AD;&#x8BF7;&#x6C42;&#x662F; <strong>POST</strong> &#x8FD8;&#x662F; <strong>GET</strong>&#x3002;&#x5982;&#x679C;&#x8BF7;&#x6C42;&#x662F; <strong>POST</strong>&#xFF0C;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x7528;&#x6237;&#x5411;&#x670D;&#x52A1;&#x5668;&#x63D0;&#x4EA4;&#x4E86;&#x4E00;&#x4E9B;&#x6570;&#x636E;&#x3002;&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x5B9E;&#x4F8B;&#x5316;&#x4E00;&#x4E2A;&#x5C06; <strong>POST</strong> &#x6570;&#x636E;&#x4F20;&#x9012;&#x7ED9; form &#x7684; form &#x5B9E;&#x4F8B;&#xFF1A;<code>form = NewTopicForm(request.POST)</code>&#x3002;</p>
<p>&#x7136;&#x540E;&#xFF0C;&#x6211;&#x4EEC;&#x8BA9; Django &#x9A8C;&#x8BC1;&#x6570;&#x636E;&#xFF0C;&#x68C0;&#x67E5; form &#x662F;&#x5426;&#x6709;&#x6548;&#xFF0C;&#x6211;&#x4EEC;&#x80FD;&#x5426;&#x5C06;&#x5176;&#x5B58;&#x5165;&#x6570;&#x636E;&#x5E93;&#xFF1A;<code>if form.is_valid():</code>&#x3002;&#x5982;&#x679C;&#x8868;&#x5355;&#x6709;&#x6548;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528; <code>form.save()</code> &#x5C06;&#x6570;&#x636E;&#x5B58;&#x5165;&#x6570;&#x636E;&#x5E93;&#x3002;<code>save()</code> &#x65B9;&#x6CD5;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x5B58;&#x5165;&#x6570;&#x636E;&#x5E93;&#x7684; Model &#x5B9E;&#x4F8B;&#x3002;
&#x6240;&#x4EE5;&#xFF0C;&#x56E0;&#x4E3A;&#x8FD9;&#x662F;&#x4E00;&#x4E2A; <strong>Topic</strong> form, &#x6240;&#x4EE5;&#x5B83;&#x4F1A;&#x8FD4;&#x56DE; <code>topic = form.save()</code> &#x521B;&#x5EFA;&#x7684; <strong>Topic</strong>&#x3002;&#x7136;&#x540E;&#xFF0C;&#x901A;&#x7528;&#x7684;&#x8DEF;&#x5F84;&#x662F;&#x628A;&#x7528;&#x6237;&#x91CD;&#x5B9A;&#x5411;&#x5230;&#x5176;&#x4ED6;&#x5730;&#x65B9;&#xFF0C;&#x4EE5;&#x907F;&#x514D;&#x7528;&#x6237;&#x901A;&#x8FC7;&#x6309; F5 &#x91CD;&#x65B0;&#x63D0;&#x4EA4;&#x8868;&#x5355;&#xFF0C;&#x5E76;&#x4E14;&#x4FDD;&#x8BC1;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x6D41;&#x7A0B;&#x8D70;&#x5411;&#x3002;</p>
<p>&#x73B0;&#x5728;&#xFF0C;&#x5982;&#x679C;&#x6570;&#x636E;&#x662F;&#x65E0;&#x6548;&#x7684;&#xFF0C;Django &#x4F1A;&#x7ED9; form &#x6DFB;&#x52A0;&#x9519;&#x8BEF;&#x5217;&#x8868;&#x3002;&#x7136;&#x540E;&#xFF0C;&#x89C6;&#x56FE;&#x51FD;&#x6570;&#x4E0D;&#x4F1A;&#x505A;&#x4EFB;&#x4F55;&#x5904;&#x7406;&#x5E76;&#x4E14;&#x8FD4;&#x56DE;&#x6700;&#x540E;&#x4E00;&#x53E5;&#xFF1A;
<code>return render(request, &apos;new_topic.html&apos;, {&apos;form&apos;: form})</code>&#x3002;&#x8FD9;&#x610F;&#x5473;&#x7740;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x66F4;&#x65B0; <strong>new_topic.html</strong> &#x4EE5;&#x663E;&#x793A;&#x9519;&#x8BEF;&#x3002;</p>
<p>&#x5982;&#x679C;&#x8BF7;&#x6C42;&#x662F; <strong>GET</strong>&#xFF0C;&#x6211;&#x4EEC;&#x53EA;&#x9700;&#x8981;&#x4F7F;&#x7528; <code>form = NewTopicForm()</code> &#x521D;&#x59CB;&#x5316;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x7A7A;&#x8868;&#x5355;&#x3002;</p>
<p>&#x8BA9;&#x6211;&#x4EEC;&#x8FD0;&#x884C;&#x6D4B;&#x8BD5;&#x5E76;&#x89C2;&#x5BDF;&#x60C5;&#x51B5;&#xFF1A;</p>
<pre><code class="lang-python">python manage.py test
</code></pre>
<pre><code class="lang-python">Creating test database <span class="hljs-keyword">for</span> alias <span class="hljs-string">&apos;default&apos;</span>...
System check identified no issues (<span class="hljs-number">0</span> silenced).
...............
----------------------------------------------------------------------
Ran <span class="hljs-number">15</span> tests <span class="hljs-keyword">in</span> <span class="hljs-number">0.522</span>s

OK
Destroying test database <span class="hljs-keyword">for</span> alias <span class="hljs-string">&apos;default&apos;</span>...
</code></pre>
<p>&#x6211;&#x4EEC;&#x751A;&#x81F3;&#x4FEE;&#x590D;&#x4E86;&#x6700;&#x540E;&#x4E24;&#x4E2A;&#x6D4B;&#x8BD5;&#x3002;</p>
<p>Django Forms API &#x4E0D;&#x4EC5;&#x4EC5;&#x662F;&#x5904;&#x7406;&#x548C;&#x9A8C;&#x8BC1;&#x6570;&#x636E;&#x3002;&#x5B83;&#x8FD8;&#x4E3A;&#x6211;&#x4EEC;&#x751F;&#x6210; HTML&#x3002;</p>
<p><strong>templates/new_topic.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base.html&apos; %}

{% block title %}Start a New Topic{% endblock %}

{% block breadcrumb %}
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;home&apos; %}&quot;</span>&gt;</span>Boards<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;board_topics&apos; board.pk %}&quot;</span>&gt;</span>{{ board.name }}<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item active&quot;</span>&gt;</span>New topic<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
{% endblock %}

{% block content %}
  <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span>&gt;</span>
    {% csrf_token %}
    {{ form.as_p }}
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-success&quot;</span>&gt;</span>Post<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
{% endblock %}
</code></pre>
<p>&#x8FD9;&#x4E2A; <code>form</code> &#x6709;&#x4E09;&#x4E2A;&#x6E32;&#x67D3;&#x9009;&#x9879;&#xFF1A;<code>form.as_table</code>&#xFF0C;<code>form.as_ul</code> &#x548C; <code>form.as_p</code>&#x3002;&#x8FD9;&#x662F;&#x4E00;&#x4E2A;&#x5FEB;&#x901F;&#x7684;&#x6E32;&#x67D3;&#x8868;&#x5355;&#x6240;&#x6709;&#x5B57;&#x6BB5;&#x7684;&#x65B9;&#x6CD5;&#x3002;&#x987E;&#x540D;&#x601D;&#x4E49;&#xFF0C;<code>as_table</code> &#x4F7F;&#x7528; table &#x6807;&#x7B7E;&#x6765;&#x683C;&#x5F0F;&#x5316;&#x8F93;&#x5165;&#xFF0C;<code>as_ul</code> &#x4F7F;&#x7528; li &#x6807;&#x7B7E;&#x3002;</p>
<p>&#x770B;&#x770B;&#x6548;&#x679C;&#xFF1A;</p>
<p><img src="statics/3-24.png" alt="3-24.png"></p>
<p>&#x6211;&#x4EEC;&#x4EE5;&#x524D;&#x7684; form &#x770B;&#x8D77;&#x6765;&#x66F4;&#x597D;&#xFF0C;&#x662F;&#x5427;&#xFF1F;&#x6211;&#x4EEC;&#x5C06;&#x7ACB;&#x5373;&#x4FEE;&#x590D;&#x5B83;&#x3002;</p>
<p>&#x5B83;&#x770B;&#x8D77;&#x6765;&#x5F88;&#x7834;&#xFF0C;&#x4F46;&#x662F;&#x76F8;&#x4FE1;&#x6211;&#xFF1B;&#x5B83;&#x80CC;&#x540E;&#x6709;&#x5F88;&#x591A;&#x4E1C;&#x897F;&#x3002;&#x5B83;&#x975E;&#x5E38;&#x5F3A;&#x5927;&#x3002;&#x6BD4;&#x5982;&#xFF0C;&#x5982;&#x679C;&#x6211;&#x4EEC;&#x7684;&#x8868;&#x5355;&#x6709; 50 &#x4E2A;&#x5B57;&#x6BB5;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x952E;&#x5165; <code>{{ form.as_p }}</code> &#x6765;&#x663E;&#x793A;&#x6240;&#x6709;&#x5B57;&#x6BB5;&#x3002;</p>
<p>&#x6B64;&#x5916;&#xFF0C;&#x4F7F;&#x7528; Forms API&#xFF0C;Django &#x4F1A;&#x9A8C;&#x8BC1;&#x6570;&#x636E;&#x5E76;&#x4E14;&#x5411;&#x6BCF;&#x4E2A;&#x5B57;&#x6BB5;&#x6DFB;&#x52A0;&#x9519;&#x8BEF;&#x6D88;&#x606F;&#x3002;&#x8BA9;&#x6211;&#x4EEC;&#x5C1D;&#x8BD5;&#x63D0;&#x4EA4;&#x4E00;&#x4E2A;&#x7A7A;&#x7684;&#x8868;&#x5355;&#xFF1A;</p>
<p><img src="statics/3-25.png" alt="3-25.png"></p>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;
&#x5982;&#x679C;&#x4F60;&#x63D0;&#x4EA4;&#x8868;&#x5355;&#x65F6;&#x770B;&#x5230;&#x7C7B;&#x4F3C;&#x8FD9;&#x6837;&#x7684;&#x4E1C;&#x897F;&#xFF1A;<img src="statics/3-26.png" alt="3-26.png">&#xFF0C;&#x8FD9;&#x4E0D;&#x662F; Django &#x5BFC;&#x81F4;&#x7684;&#xFF0C;&#x800C;&#x662F;&#x4F60;&#x7684;&#x6D4F;&#x89C8;&#x5668;&#x8FDB;&#x884C;&#x9884;&#x9A8C;&#x8BC1;&#x3002;&#x8981;&#x7981;&#x7528;&#x5B83;&#x53EF;&#x4EE5;&#x5728;&#x4F60;&#x7684;&#x8868;&#x5355;&#x6807;&#x7B7E;&#x4E2D;&#x6DFB;&#x52A0; <strong>novalidate</strong> &#x5C5E;&#x6027;&#xFF1A;<strong><form method="post" novalidate=""></form></strong></p>
<p>&#x4F60;&#x53EF;&#x4EE5;&#x4E0D;&#x4FEE;&#x6539;&#x5B83;&#xFF0C;&#x4E0D;&#x4F1A;&#x6709;&#x95EE;&#x9898;&#x3002;&#x8FD9;&#x53EA;&#x662F;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x7684;&#x8868;&#x5355;&#x73B0;&#x5728;&#x975E;&#x5E38;&#x7B80;&#x5355;&#xFF0C;&#x800C;&#x4E14;&#x6211;&#x4EEC;&#x6CA1;&#x6709;&#x592A;&#x591A;&#x7684;&#x6570;&#x636E;&#x9A8C;&#x8BC1;&#x53EF;&#x4EE5;&#x770B;&#x5230;&#x3002;</p>
<p>&#x53E6;&#x5916;&#x4E00;&#x4EF6;&#x9700;&#x8981;&#x6CE8;&#x610F;&#x7684;&#x4E8B;&#x60C5;&#x662F;&#xFF1A;&#x6CA1;&#x6709; &#x201C;&#x53EA;&#x5BA2;&#x6237;&#x7AEF;&#x9A8C;&#x8BC1;&#x201D; &#x8FD9;&#x6837;&#x7684;&#x4E8B;&#x60C5;&#x3002;JavaScript &#x9A8C;&#x8BC1;&#x6216;&#x8005;&#x6D4F;&#x89C8;&#x5668;&#x9A8C;&#x8BC1;&#x4EC5;&#x7528;&#x4E8E;&#x53EF;&#x7528;&#x6027;&#x76EE;&#x7684;&#x3002;&#x540C;&#x65F6;&#x4E5F;&#x51CF;&#x5C11;&#x4E86;&#x5BF9;&#x670D;&#x52A1;&#x5668;&#x7684;&#x8BF7;&#x6C42;&#x6570;&#x91CF;&#x3002;&#x6570;&#x636E;&#x9A8C;&#x8BC1;&#x5E94;&#x8BE5;&#x59CB;&#x7EC8;&#x5728;&#x670D;&#x52A1;&#x5668;&#x7AEF;&#x5B8C;&#x6210;&#xFF0C;&#x8FD9;&#x6837;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5B8C;&#x5168;&#x638C;&#x63A7;&#x6570;&#x636E;&#x3002;
`</p>
</blockquote>
<p>&#x5B83;&#x8FD8;&#x53EF;&#x4EE5;&#x5904;&#x7406;&#x5728; <strong>Form</strong> &#x7C7B;&#x6216;&#x8005; <strong>Model</strong> &#x7C7B;&#x4E2D;&#x5B9A;&#x4E49;&#x7684;&#x5E2E;&#x52A9;&#x6587;&#x672C;&#x3002;</p>
<p><strong>boards/forms.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django <span class="hljs-keyword">import</span> forms
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Topic

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NewTopicForm</span><span class="hljs-params">(forms.ModelForm)</span>:</span>
    message = forms.CharField(
        widget=forms.Textarea(),
        max_length=<span class="hljs-number">4000</span>,
        help_text=<span class="hljs-string">&apos;The max length of the text is 4000.&apos;</span>
    )

    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        model = Topic
        fields = [<span class="hljs-string">&apos;subject&apos;</span>, <span class="hljs-string">&apos;message&apos;</span>]
</code></pre>
<p><img src="statics/3-27.png" alt="3-27.png"></p>
<p>&#x6211;&#x4EEC;&#x4E5F;&#x53EF;&#x4EE5;&#x4E3A;&#x8868;&#x5355;&#x5B57;&#x6BB5;&#x8BBE;&#x7F6E;&#x989D;&#x5916;&#x7684;&#x5C5E;&#x6027;&#xFF1A;</p>
<p><strong>boards/forms.py</strong></p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django <span class="hljs-keyword">import</span> forms
<span class="hljs-keyword">from</span> .models <span class="hljs-keyword">import</span> Topic

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NewTopicForm</span><span class="hljs-params">(forms.ModelForm)</span>:</span>
    message = forms.CharField(
        widget=forms.Textarea(
            attrs={<span class="hljs-string">&apos;rows&apos;</span>: <span class="hljs-number">5</span>, <span class="hljs-string">&apos;placeholder&apos;</span>: <span class="hljs-string">&apos;What is on your mind?&apos;</span>}
        ),
        max_length=<span class="hljs-number">4000</span>,
        help_text=<span class="hljs-string">&apos;The max length of the text is 4000.&apos;</span>
    )

    <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Meta</span>:</span>
        model = Topic
        fields = [<span class="hljs-string">&apos;subject&apos;</span>, <span class="hljs-string">&apos;message&apos;</span>]
</code></pre>
<p><img src="statics/3-28.png" alt="3-28.png"></p>
<h3 id="&#x7528;bootstrap-&#x8868;&#x5355;&#x6E32;&#x67D3;">&#x7528;BootStrap &#x8868;&#x5355;&#x6E32;&#x67D3;</h3>
<p>&#x73B0;&#x5728;&#x8BA9;&#x6211;&#x4EEC;&#x628A;&#x4E8B;&#x60C5;&#x505A;&#x5F97;&#x66F4;&#x5B8C;&#x5584;&#x3002;</p>
<p>&#x5F53;&#x4F7F;&#x7528; Bootstrap &#x6216;&#x8005;&#x5176;&#x4ED6;&#x7684;&#x524D;&#x7AEF;&#x5E93;&#x65F6;&#xFF0C;&#x6211;&#x6BD4;&#x8F83;&#x559C;&#x6B22;&#x4F7F;&#x7528;&#x4E00;&#x4E2A;&#x53EB;&#x505A; <strong>django-widget-tweaks</strong> &#x7684; Django &#x5E93;&#x3002;&#x5B83;&#x53EF;&#x4EE5;&#x8BA9;&#x6211;&#x4EEC;&#x66F4;&#x597D;&#x5730;&#x63A7;&#x5236;&#x6E32;&#x67D3;&#x7684;&#x5904;&#x7406;&#xFF0C;&#x5728;&#x4FDD;&#x8BC1;&#x9ED8;&#x8BA4;&#x503C;&#x7684;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x53EA;&#x9700;&#x5728;&#x4E0A;&#x9762;&#x6DFB;&#x52A0;&#x989D;&#x5916;&#x7684;&#x81EA;&#x5B9A;&#x4E49;&#x8BBE;&#x7F6E;&#x3002;</p>
<p>&#x5F00;&#x59CB;&#x5B89;&#x88C5;&#x5B83;&#xFF1A;</p>
<pre><code class="lang-python">pip install django-widget-tweaks
</code></pre>
<p>&#x6DFB;&#x52A0;&#x5230; <code>INSTALLED_APPS</code>&#xFF1A;</p>
<p><strong>myproject/settings.py</strong></p>
<pre><code class="lang-python">INSTALLED_APPS = [
    <span class="hljs-string">&apos;django.contrib.admin&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.auth&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.contenttypes&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.sessions&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.messages&apos;</span>,
    <span class="hljs-string">&apos;django.contrib.staticfiles&apos;</span>,

    <span class="hljs-string">&apos;widget_tweaks&apos;</span>,

    <span class="hljs-string">&apos;boards&apos;</span>,
]
</code></pre>
<p>&#x73B0;&#x5728;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;&#x5B83;&#x4E86;&#xFF1A;</p>
<p><strong>templates/new_topic.html</strong></p>
<pre><code class="lang-python">{% extends &apos;base.html&apos; %}

{% load widget_tweaks %}

{% block title %}Start a New Topic{% endblock %}

{% block breadcrumb %}
  &lt;li class=&quot;breadcrumb-item&quot;&gt;&lt;a href=&quot;{% url &apos;home&apos; %}&quot;&gt;Boards&lt;/a&gt;&lt;/li&gt;
  &lt;li class=&quot;breadcrumb-item&quot;&gt;&lt;a href=&quot;{% url &apos;board_topics&apos; board.pk %}&quot;&gt;{{ board.name }}&lt;/a&gt;&lt;/li&gt;
  &lt;li class=&quot;breadcrumb-item active&quot;&gt;New topic&lt;/li&gt;
{% endblock %}

{% block content %}
  &lt;form method=&quot;post&quot; novalidate&gt;
    {% csrf_token %}

    {% for field in form %}
      &lt;div class=&quot;form-group&quot;&gt;
        {{ field.label_tag }}

        {% render_field field class=&quot;form-control&quot; %}

        {% if field.help_text %}
          &lt;small class=&quot;form-text text-muted&quot;&gt;
            {{ field.help_text }}
          &lt;/small&gt;
        {% endif %}
      &lt;/div&gt;
    {% endfor %}

    &lt;button type=&quot;submit&quot; class=&quot;btn btn-success&quot;&gt;Post&lt;/button&gt;
  &lt;/form&gt;
{% endblock %}
</code></pre>
<p><img src="statics/3-29.png" alt="3-29.png"></p>
<p>&#x8FD9;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x4F7F;&#x7528;&#x7684; <strong>django-widget-tweaks</strong> &#x7684;&#x6548;&#x679C;&#x3002;&#x9996;&#x5148;&#xFF0C;&#x6211;&#x4EEC;&#x4F7F;&#x7528; <strong>{% load widget_tweaks %}</strong> &#x6A21;&#x677F;&#x6807;&#x7B7E;&#x5C06;&#x5176;&#x52A0;&#x8F7D;&#x5230;&#x6A21;&#x677F;&#x3002;&#x7136;&#x540E;&#x8FD9;&#x6837;&#x4F7F;&#x7528;&#x5B83;&#xFF1A;</p>
<pre><code class="lang-html">{% render_field field class=&quot;form-control&quot; %}
</code></pre>
<p><code>render_field</code> &#x4E0D;&#x5C5E;&#x4E8E; Django&#xFF1B;&#x5B83;&#x5B58;&#x5728;&#x4E8E;&#x6211;&#x4EEC;&#x5B89;&#x88C5;&#x7684;&#x5305;&#x91CC;&#x9762;&#x3002;&#x8981;&#x4F7F;&#x7528;&#x5B83;&#xFF0C;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4F20;&#x9012;&#x4E00;&#x4E2A;&#x8868;&#x5355;&#x57DF;&#x5B9E;&#x4F8B;&#x4F5C;&#x4E3A;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#xFF0C;&#x7136;&#x540E;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;&#x4EFB;&#x610F;&#x7684; HTML &#x5C5E;&#x6027;&#x53BB;&#x8865;&#x5145;&#x5B83;&#x3002;&#x8FD9;&#x5F88;&#x6709;&#x7528;&#x56E0;&#x4E3A;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6839;&#x636E;&#x7279;&#x5B9A;&#x7684;&#x6761;&#x4EF6;&#x6307;&#x5B9A;&#x7C7B;&#x3002;</p>
<p>&#x4E00;&#x4E9B; <code>render_field</code> &#x6A21;&#x677F;&#x6807;&#x7B7E;&#x7684;&#x4F8B;&#x5B50;&#xFF1A;</p>
<pre><code class="lang-html">{% render_field form.subject class=&quot;form-control&quot; %}
{% render_field form.message class=&quot;form-control&quot; placeholder=form.message.label %}
{% render_field field class=&quot;form-control&quot; placeholder=&quot;Write a message!&quot; %}
{% render_field field style=&quot;font-size: 20px&quot; %}
</code></pre>
<p>&#x73B0;&#x5728;&#x8981;&#x5B9E;&#x73B0; Bootstrap 4 &#x9A8C;&#x8BC1;&#x6807;&#x7B7E;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x4FEE;&#x6539; <strong>new_topic.html</strong> &#x6A21;&#x677F;&#x3002;</p>
<p><strong>templates/new_topic.html</strong></p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span> <span class="hljs-attr">novalidate</span>&gt;</span>
  {% csrf_token %}

  {% for field in form %}
    <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-group&quot;</span>&gt;</span>
      {{ field.label_tag }}

      {% if form.is_bound %}
        {% if field.errors %}

          {% render_field field class=&quot;form-control is-invalid&quot; %}
          {% for error in field.errors %}
            <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;invalid-feedback&quot;</span>&gt;</span>
              {{ error }}
            <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
          {% endfor %}

        {% else %}
          {% render_field field class=&quot;form-control is-valid&quot; %}
        {% endif %}
      {% else %}
        {% render_field field class=&quot;form-control&quot; %}
      {% endif %}

      {% if field.help_text %}
        <span class="hljs-tag">&lt;<span class="hljs-name">small</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-text text-muted&quot;</span>&gt;</span>
          {{ field.help_text }}
        <span class="hljs-tag">&lt;/<span class="hljs-name">small</span>&gt;</span>
      {% endif %}
    <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
  {% endfor %}

  <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-success&quot;</span>&gt;</span>Post<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
</code></pre>
<p>&#x6548;&#x679C;&#x662F;&#xFF1A;</p>
<p><img src="statics/3-30.png" alt="3-30.png"></p>
<p><img src="statics/3-31.png" alt="3-31.png"></p>
<p>&#x6240;&#x4EE5;&#xFF0C;&#x6211;&#x4EEC;&#x6709;&#x4E09;&#x79CD;&#x4E0D;&#x540C;&#x7684;&#x6E32;&#x67D3;&#x72B6;&#x6001;&#xFF1A;</p>
<ul>
<li><strong>Initial state</strong>&#xFF1A;&#x8868;&#x5355;&#x6CA1;&#x6709;&#x6570;&#x636E;(&#x4E0D;&#x53D7;&#x7EA6;&#x675F;)</li>
<li><strong>Invalid</strong>&#xFF1A;&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x4E86; <code>.is-invalid</code> &#x8FD9;&#x4E2A; CSS class &#x5E76;&#x5C06;&#x9519;&#x8BEF;&#x6D88;&#x606F;&#x6DFB;&#x52A0;&#x5230;&#x5177;&#x6709; <code>.invalid-feedback</code> class &#x7684;&#x5143;&#x7D20;&#x4E2D;</li>
<li><strong>Valid</strong>&#xFF1A;&#x6211;&#x4EEC;&#x6DFB;&#x52A0;&#x4E86; <code>.is-valid</code> &#x7684; CSS class&#xFF0C;&#x4EE5;&#x7EFF;&#x8272;&#x7ED8;&#x5236;&#x8868;&#x5355;&#x57DF;&#xFF0C;&#x5E76;&#x5411;&#x7528;&#x6237;&#x53CD;&#x9988;&#x5B83;&#x662F;&#x5426;&#x53EF;&#x884C;&#x3002;</li>
</ul>
<h3 id="&#x590D;&#x7528;&#x8868;&#x5355;&#x6A21;&#x677F;">&#x590D;&#x7528;&#x8868;&#x5355;&#x6A21;&#x677F;</h3>
<p>&#x6A21;&#x677F;&#x770B;&#x8D77;&#x6765;&#x6709;&#x70B9;&#x590D;&#x6742;&#xFF0C;&#x662F;&#x5427;&#xFF1F;&#x6709;&#x4E2A;&#x597D;&#x6D88;&#x606F;&#x662F;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5728;&#x9879;&#x76EE;&#x4E2D;&#x91CD;&#x590D;&#x4F7F;&#x7528;&#x5B83;&#x3002;</p>
<p>&#x5728; <strong>templates</strong> &#x6587;&#x4EF6;&#x5939;&#x4E2D;&#xFF0C;&#x521B;&#x5EFA;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x6587;&#x4EF6;&#x5939;&#x547D;&#x540D;&#x4E3A; <strong>includes</strong>&#xFF1A;</p>
<pre><code class="lang-shell">myproject/
 |-- myproject/
 |    |-- boards/
 |    |-- myproject/
 |    |-- templates/
 |    |    |-- includes/    &lt;-- here!
 |    |    |-- base.html
 |    |    |-- home.html
 |    |    |-- new_topic.html
 |    |    +-- topics.html
 |    +-- manage.py
 +-- venv/
</code></pre>
<p>&#x5728; <strong>includes</strong> &#x6587;&#x4EF6;&#x5939;&#x4E2D;&#xFF0C;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; <strong>form.html</strong>&#xFF1A;</p>
<p><strong>templates/includes/form.html</strong></p>
<pre><code class="lang-html">{% load widget_tweaks %}

{% for field in form %}
  <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-group&quot;</span>&gt;</span>
    {{ field.label_tag }}

    {% if form.is_bound %}
      {% if field.errors %}
        {% render_field field class=&quot;form-control is-invalid&quot; %}
        {% for error in field.errors %}
          <span class="hljs-tag">&lt;<span class="hljs-name">div</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;invalid-feedback&quot;</span>&gt;</span>
            {{ error }}
          <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
        {% endfor %}
      {% else %}
        {% render_field field class=&quot;form-control is-valid&quot; %}
      {% endif %}
    {% else %}
      {% render_field field class=&quot;form-control&quot; %}
    {% endif %}

    {% if field.help_text %}
      <span class="hljs-tag">&lt;<span class="hljs-name">small</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;form-text text-muted&quot;</span>&gt;</span>
        {{ field.help_text }}
      <span class="hljs-tag">&lt;/<span class="hljs-name">small</span>&gt;</span>
    {% endif %}
  <span class="hljs-tag">&lt;/<span class="hljs-name">div</span>&gt;</span>
{% endfor %}
</code></pre>
<p>&#x73B0;&#x5728;&#x6765;&#x4FEE;&#x6539;&#x6211;&#x4EEC;&#x7684; <strong>new_topic.html</strong> &#x6A21;&#x677F;&#xFF1A;</p>
<p><strong>templates/new_topic.html</strong></p>
<pre><code class="lang-html">{% extends &apos;base.html&apos; %}

{% block title %}Start a New Topic{% endblock %}

{% block breadcrumb %}
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;home&apos; %}&quot;</span>&gt;</span>Boards<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item&quot;</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">&quot;{% url &apos;board_topics&apos; board.pk %}&quot;</span>&gt;</span>{{ board.name }}<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span><span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-name">li</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;breadcrumb-item active&quot;</span>&gt;</span>New topic<span class="hljs-tag">&lt;/<span class="hljs-name">li</span>&gt;</span>
{% endblock %}

{% block content %}
  <span class="hljs-tag">&lt;<span class="hljs-name">form</span> <span class="hljs-attr">method</span>=<span class="hljs-string">&quot;post&quot;</span> <span class="hljs-attr">novalidate</span>&gt;</span>
    {% csrf_token %}
    {% include &apos;includes/form.html&apos; %}
    <span class="hljs-tag">&lt;<span class="hljs-name">button</span> <span class="hljs-attr">type</span>=<span class="hljs-string">&quot;submit&quot;</span> <span class="hljs-attr">class</span>=<span class="hljs-string">&quot;btn btn-success&quot;</span>&gt;</span>Post<span class="hljs-tag">&lt;/<span class="hljs-name">button</span>&gt;</span>
  <span class="hljs-tag">&lt;/<span class="hljs-name">form</span>&gt;</span>
{% endblock %}
</code></pre>
<p>&#x987E;&#x540D;&#x601D;&#x4E49;&#xFF0C;<code>{% include %}</code> &#x7528;&#x6765;&#x5728;&#x5176;&#x4ED6;&#x7684;&#x6A21;&#x677F;&#x4E2D;&#x5305;&#x542B; HTML &#x6A21;&#x677F;&#x3002;&#x8FD9;&#x662F;&#x5728;&#x9879;&#x76EE;&#x4E2D;&#x91CD;&#x7528; HTML &#x7EC4;&#x4EF6;&#x7684;&#x5E38;&#x7528;&#x65B9;&#x6CD5;&#x3002;</p>
<p>&#x5728;&#x4E0B;&#x4E00;&#x4E2A;&#x6211;&#x4EEC;&#x5B9E;&#x73B0;&#x7684;&#x8868;&#x5355;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7B80;&#x5355;&#x5730;&#x4F7F;&#x7528; <code>{% include &apos;includes/form.html&apos; %}</code> &#x53BB;&#x6E32;&#x67D3;&#x5B83;&#x3002;</p>
<p><strong>Adding More Tests</strong></p>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x5728;&#x4F7F;&#x7528; <strong>Django &#x8868;&#x5355;</strong>&#xFF1B;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x6DFB;&#x52A0;&#x66F4;&#x591A;&#x7684;&#x6D4B;&#x8BD5;&#x4EE5;&#x786E;&#x4FDD;&#x5B83;&#x80FD;&#x8FD0;&#x884C;&#x987A;&#x5229;</p>
<p><strong>boards/tests.py</strong></p>
<pre><code class="lang-python"><span class="hljs-comment"># ... other imports</span>
<span class="hljs-keyword">from</span> .forms <span class="hljs-keyword">import</span> NewTopicForm

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">NewTopicTests</span><span class="hljs-params">(TestCase)</span>:</span>
    <span class="hljs-comment"># ... other tests</span>

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_contains_form</span><span class="hljs-params">(self)</span>:</span>  <span class="hljs-comment"># &lt;- new test</span>
        url = reverse(<span class="hljs-string">&apos;new_topic&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">1</span>})
        response = self.client.get(url)
        form = response.context.get(<span class="hljs-string">&apos;form&apos;</span>)
        self.assertIsInstance(form, NewTopicForm)

    <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">test_new_topic_invalid_post_data</span><span class="hljs-params">(self)</span>:</span>  <span class="hljs-comment"># &lt;- updated this one</span>
        <span class="hljs-string">&apos;&apos;&apos;
        Invalid post data should not redirect
        The expected behavior is to show the form again with validation errors
        &apos;&apos;&apos;</span>
        url = reverse(<span class="hljs-string">&apos;new_topic&apos;</span>, kwargs={<span class="hljs-string">&apos;pk&apos;</span>: <span class="hljs-number">1</span>})
        response = self.client.post(url, {})
        form = response.context.get(<span class="hljs-string">&apos;form&apos;</span>)
        self.assertEquals(response.status_code, <span class="hljs-number">200</span>)
        self.assertTrue(form.errors)
</code></pre>
<p>&#x8FD9;&#x662F;&#x6211;&#x4EEC;&#x7B2C;&#x4E00;&#x6B21;&#x4F7F;&#x7528; <code>assertIsInstance</code> &#x65B9;&#x6CD5;&#x3002;&#x57FA;&#x672C;&#x4E0A;&#x6211;&#x4EEC;&#x7684;&#x5904;&#x7406;&#x662F;&#x6293;&#x53D6;&#x4E0A;&#x4E0B;&#x6587;&#x7684;&#x8868;&#x5355;&#x5B9E;&#x4F8B;&#xFF0C;&#x68C0;&#x67E5;&#x5B83;&#x662F;&#x5426;&#x662F;&#x4E00;&#x4E2A; <code>NewTopicForm</code>&#x3002;&#x5728;&#x6700;&#x540E;&#x7684;&#x6D4B;&#x8BD5;&#x4E2D;&#xFF0C;&#x6211;&#x6DFB;&#x52A0;&#x4E86; <code>self.assertTrue(form.errors)</code> &#x4EE5;&#x786E;&#x4FDD;&#x6570;&#x636E;&#x65E0;&#x6548;&#x7684;&#x65F6;&#x5019;&#x8868;&#x5355;&#x4F1A;&#x663E;&#x793A;&#x9519;&#x8BEF;&#x3002;</p>
<h3 id="&#x603B;&#x7ED3;">&#x603B;&#x7ED3;</h3>
<p>&#x5728;&#x8FD9;&#x4E2A;&#x8BFE;&#x7A0B;&#xFF0C;&#x6211;&#x4EEC;&#x5B66;&#x4E60;&#x4E86; URLs, &#x53EF;&#x91CD;&#x7528;&#x6A21;&#x677F;&#x548C;&#x8868;&#x5355;&#x3002;&#x50CF;&#x5F80;&#x5E38;&#x4E00;&#x6837;&#xFF0C;&#x6211;&#x4EEC;&#x4E5F;&#x5B9E;&#x73B0;&#x4E86;&#x51E0;&#x4E2A;&#x6D4B;&#x8BD5;&#x7528;&#x4F8B;&#x3002;&#x8FD9;&#x80FD;&#x4F7F;&#x6211;&#x4EEC;&#x5F00;&#x53D1;&#x4E2D;&#x66F4;&#x81EA;&#x4FE1;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x7684;&#x6D4B;&#x8BD5;&#x6587;&#x4EF6;&#x53D8;&#x7684;&#x8D8A;&#x6765;&#x8D8A;&#x5927;&#xFF0C;&#x6240;&#x4EE5;&#x5728;&#x4E0B;&#x4E00;&#x8282;&#x4E2D;&#xFF0C;&#x6211;&#x4EEC;&#x91CD;&#x6784;&#x5B83;&#x4EE5;&#x63D0;&#x9AD8;&#x5B83;&#x7684;&#x53EF;&#x7EF4;&#x62A4;&#x6027;&#xFF0C;&#x4ECE;&#x800C;&#x7EF4;&#x6301;&#x6211;&#x4EEC;&#x4EE3;&#x7801;&#x7684;&#x589E;&#x52A0;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x4E5F;&#x8FBE;&#x5230;&#x4E86;&#x6211;&#x4EEC;&#x9700;&#x8981;&#x4E0E;&#x767B;&#x5F55;&#x7684;&#x7528;&#x6237;&#x8FDB;&#x884C;&#x4EA4;&#x4E92;&#x7684;&#x76EE;&#x7684;&#x3002;&#x5728;&#x4E0B;&#x4E00;&#x8282;&#xFF0C;&#x6211;&#x4EEC;&#x5B66;&#x4E60;&#x4E86;&#x5173;&#x4E8E;&#x8BA4;&#x8BC1;&#x7684;&#x4E00;&#x5207;&#x77E5;&#x8BC6;&#x548C;&#x600E;&#x4E48;&#x53BB;&#x4FDD;&#x62A4;&#x6211;&#x4EEC;&#x7684;&#x89C6;&#x56FE;&#x548C;&#x8D44;&#x6E90;&#x3002;</p>
<p>&#x8BE5;&#x9879;&#x76EE;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#x5728; GitHub &#x53EF;&#x7528;&#x3002;&#x9879;&#x76EE;&#x7684;&#x5F53;&#x524D;&#x72B6;&#x6001;&#x53EF;&#x4EE5;&#x5728;&#x53D1;&#x5E03;&#x6807;&#x7B7E; <strong>v0.3-lw</strong> &#x4E0B;&#x627E;&#x5230;&#x3002;&#x4E0B;&#x9762;&#x662F;&#x94FE;&#x63A5;&#xFF1A;</p>
<p><a href="https://github.com/sibtc/django-beginners-guide/tree/v0.3-lw" target="_blank">https://github.com/sibtc/django-beginners-guide/tree/v0.3-lw</a></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="AdvancedConcept2.html" class="navigation navigation-prev " aria-label="Previous page: Django入门与实践-第12章：复用模板">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="Authentication0.html" class="navigation navigation-next " aria-label="Next page: Django入门与实践-第14章：用户注册">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Django入门与实践-第13章：表单处理","level":"1.14","depth":1,"next":{"title":"Django入门与实践-第14章：用户注册","level":"1.15","depth":1,"path":"Authentication0.md","ref":"./Authentication0.md","articles":[]},"previous":{"title":"Django入门与实践-第12章：复用模板","level":"1.13","depth":1,"path":"AdvancedConcept2.md","ref":"./AdvancedConcept2.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["include-codeblock","livereload"],"pluginsConfig":{"include-codeblock":{"check":false,"edit":false,"fixlang":false,"lang":"","template":"default","theme":"chrome","unindent":false},"livereload":{},"highlight":{},"search":{},"lunr":{"ignoreSpecialCharacters":false,"maxIndexSize":1000000},"sharing":{"all":["facebook","google","instapaper","twitter","weibo"],"facebook":true,"google":false,"instapaper":false,"twitter":true,"vk":false,"weibo":false},"fontsettings":{"family":"sans","size":2,"theme":"white"},"theme-default":{"showLevel":false,"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"ebook":"styles/ebook.css","epub":"styles/epub.css","mobi":"styles/mobi.css","pdf":"styles/pdf.css","print":"styles/print.css","website":"styles/website.css"}},"file":{"path":"AdvancedConcept3.md","mtime":"2018-05-13T13:48:46.852Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2018-05-13T12:52:49.928Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

